关于 node.js:Nodejs 驱动支持哪些聚合游标方法?
What aggregation cursor methods are supported by Nodejs drivers?
从 2.6 开始,Mongodb aggregate() 操作返回一个游标,但是其行为与从 find() 返回的普通游标有点不同。我正在使用本机 mongodb nodejs 驱动程序,但找不到有关可用聚合游标方法的适当文档。
例如,不能在聚合游标上运行 count(),但是有两种方法,例如 cursor.objsLeftInBatch() 和 cursor.itcount() n mongo shell。我在 nodejs 本机驱动程序的源代码中找不到它们中的任何一个。 Nodejs 原生驱动或 Mongoose 支持哪些聚合游标方法?
相关讨论
- 我一直在寻找的问题。
实际上从聚合中返回的是一个带有一些其他便利方法的节点转换流接口,特别是:
|
1
2 3 4 5 |
explain: [Function],
get: [Function], getOne: [Function], each: [Function], next: [Function], |
你可以通过简单地使用 console.log 转储光标对象来获得。这些应该是不言自明的, get() 方法等同于 .toArray().
因为这是一个标准的流接口,所以方法和事件处理程序可以根据这个接口使用,所以举个例子:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost/test", function(err,db) { var items = []; var cursor = db.collection('tags').aggregate( console.log( cursor ); cursor.on('data', function(data) { cursor.on('end', function() { }); |
每次光标迭代都会触发 "data" 事件,对象的属性将显示流是完整的还是仍在迭代等等。