ArangoDB AQL(ArangoDB Query Language)是一種用于查詢和操作圖數(shù)據(jù)庫的聲明式語言。為了簡化代碼,您可以使用以下方法:
const query = `FOR doc IN myCollection WHERE doc.age > @minAge AND doc.city == @city RETURN doc`;
db.query(query, { minAge: 25, city: "New York" });
const query = `
LET totalAge = (
FOR doc IN myCollection
RETURN doc.age
).sum();
RETURN totalAge;
`;
db.query(query);
const getUsersByAgeAndCity = String(function (minAge, city) {
const query = `
FOR doc IN myCollection
WHERE doc.age > ${minAge} AND doc.city == "${city}"
RETURN doc
`;
return db.query(query).next().id;
});
const userId = getUsersByAgeAndCity(25, "New York");
LENGTH()
函數(shù)獲取字符串長度:const query = `
FOR doc IN myCollection
FILTER LENGTH(doc.name) > 3
RETURN doc
`;
db.query(query);
通過這些方法,您可以簡化ArangoDB AQL代碼,使其更易讀、易維護(hù)和可擴(kuò)展。