FOR
子句的变量来表示参数。例如:const query = `FOR doc IN myCollection WHERE doc.age >= @minAge AND doc.age <= @maxAge RETURN doc`; const result = db.query(query, { minAge: 20, maxAge: 30 });
const query = `FOR doc IN myCollection WHERE doc.name =~ /^J/ RETURN doc`; const result = db.query(query);
SUM()
, AVG()
, MIN()
, MAX()
等,可以对数据进行分组和汇总。例如:const query = `FOR doc IN myCollection GROUP BY doc.category SUM(doc.value) AS totalValue RETURN { category: doc.category, totalValue: totalValue }`; const result = db.query(query);
myCollection
集合的age
属性创建一个哈希索引:db.collection('myCollection').createIndex({ type: 'hash', fields: ['age'] });
const query = `FOR doc IN myCollection FILTER doc.age >= 20 RETURN { name: doc.name, age: doc.age }`; const result = db.query(query);
SKIP()
和LIMIT()
子句进行分页。例如,每页显示10条记录,获取第2页的数据:const query = `FOR doc IN myCollection FILTER doc.age >= 20 LIMIT 10 SKIP 10 RETURN doc`; const result = db.query(query);
通过灵活运用这些技巧,可以根据需求构建高效的动态查询。