ArangoDb - 如何在限制之前计算过滤结果的数量

ArangoDb - How to count number of filtered results before limiting them

db.query(aql `
   FOR doc IN ${collection} 
     FILTER REGEX_TEST(CONCAT(VALUES(doc, true)), ${queryStr}, true)
       SORT doc[${sortBy}] ${dir}
         LIMIT ${start, count} 
           RETURN doc._key
        `)
        .then(cursor => {
            cb(cursor._result)
        }, err => console.log(err))

我有上面的 AQL 查询, 我想在限制每页结果之前计算过滤结果的总数(用于分页目的)

我认为问题与此类似, Find total number of results in mySQL query with offset+limit

想用 AQL 在 ArangoDb 中做

部分解决方案可能是这个How to count number of elements with AQL?

那么,efficient/best 满足我的 AQL 要求的解决方案是什么?

您可以在 选项 中将标志 fullCount 设置为真。然后结果将有一个额外的属性,带有子属性 statsfullCount.

然后您可以通过 cursor.extra.stats.fullCount 获得 fullCount 属性。此属性包含在应用查询中的最后一个 LIMIT 之前结果中的文档数。 see HTTP documentation

此外,您应该使用 explain feature 来分析您的查询。在您的情况下,您的查询将始终进行完整的集合扫描,因此无法很好地扩展。

更新

我在您的代码中添加了 fullCount 标志。请记住,仅当 LIMIT 之前的结果数高于

之后的结果数时,才会出现 fullCount 属性
db.query(aql ` 
  FOR doc IN ${collection} 
    FILTER REGEX_TEST(CONCAT(VALUES(doc, true)), ${queryStr}, true)
      SORT doc[${sortBy}] ${dir}
        LIMIT ${start, count} 
          RETURN {family: doc.family, group: doc.group} `, {count:true, options:{fullCount:true} })
.then(cursor => { console.log(cursor) }, err => console.log(err))