简体   繁体   English

随后的 MongoDB aggregate() 调用对上一次调用的结果进行操作?? (使用 NodeJS 驱动程序)

[英]Subsequent MongoDB aggregate() calls operate on the result of the previous call!? (using NodeJS driver)

Consider the following code:考虑以下代码:

const db = mongodb.getDb();
            
// Results count
let count = (await db.collection('sec').aggregate(aggregateQuery)
    .group({ _id: null, count: { $sum: 1 } })
    .project({ _id: 0, count: 1 })
    .toArray());
console.dir(count)

// test
let test = await db.collection('sec').aggregate(aggregateQuery).toArray();
console.dir(test)

The aggregate(aggregateQuery) filters the collection ad performs a lookup with another collection. aggregate(aggregateQuery)过滤集合广告执行与另一个集合的查找。 The first query counts all documents resulting from the aggregate, and it outputs [ { count: 2086 } ] as expected.第一个查询对聚合产生的所有文档进行计数,并按预期输出[ { count: 2086 } ]
In my mind, test should contain a bunch of documents filtered from the collection (according to the aggregate operations), but it is just [ { count: 2086 } ] , as in the first query!在我看来, test应该包含从集合中过滤出来的一堆文档(根据聚合操作),但它只是[ { count: 2086 } ] ,就像在第一个查询中一样!

If in the second query instead of aggregate(aggregateQuery) I put find() , it outputs all the collection's documents, as expected, so I guess there is something I am missing in the aggregate() function...如果在第二个查询而不是aggregate(aggregateQuery)我放find() ,它会按预期输出所有集合的文档,所以我想我在aggregate() function 中缺少一些东西......

The object passed as a parameter inside aggregate ( aggregateQuery in this case) is modified by the following functions ( group and project ), so when it is used again in the second aggregate call, it is different and yields different results. object 在aggregate (本例中为aggregateQuery )中作为参数传递的 object 被以下函数( groupproject )修改,因此在第二次aggregate调用中再次使用时,它会有所不同并产生不同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM