Nodejs驱动程序支持哪些聚合游标方法? [英] What aggregation cursor methods are supported by Nodejs drivers?
问题描述
从2.6在Mongodb上进行的aggregate()
操作返回一个游标,但是
As you know from 2.6 on Mongodb aggregate()
operation returns a cursor, however the behavior is a bit different than the normal cursor which returns from a find()
. I am using native mongodb nodejs driver, and could not find a proper documentation on available aggregate cursor methods.
例如,不能在聚合游标上运行count()
,但是有两种方法,例如cursor.objsLeftInBatch()
和cursor.itcount()
n mongo shell.我在nodejs本机驱动程序的源代码中找不到任何一个. Nodejs本机驱动程序或Mongoose支持哪些聚合游标方法?
For example, one cannot run a count()
on an aggregation cursor however there are two methods such cursor.objsLeftInBatch()
and cursor.itcount()
n mongo shell. I could not find any of them in the source code of nodejs native driver. What aggregation cursor methods are supported by Nodejs native driver or Mongoose?
推荐答案
使用游标从聚合实际返回的实际上是节点转换流接口和其他一些便捷方法,尤其是:
What actually gets returned from aggregate with a cursor is a node transform stream interface with a few other convenience methods, notably:
explain: [Function],
get: [Function],
getOne: [Function],
each: [Function],
next: [Function],
您可以通过使用console.log
简单地转储光标对象来获得.这些应该是不言自明的,与.toArray()
等效的get()
方法.
Which you can obtain by simply dumping the cursor object using console.log
. Those should be self explanatory with the get()
method being equivalent to .toArray()
.
由于这是一个标准的流接口,因此每个接口都可以使用方法和事件处理程序,因此请举一个示例:
Since this is a standard streaming interface the methods and event handlers are available as per this interface, so with an example:
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost/test", function(err,db) {
var items = [];
var counter = 0;
var cursor = db.collection('tags').aggregate(
[
{ "$project": {
"t1": 1,
"t2": 1
}}
],
{ "cursor": { "batchSize": 25 } }
);
console.log( cursor );
cursor.on('data', function(data) {
console.log( this ); // dump the current state info
items.push( data );
counter++;
});
cursor.on('end', function() {
console.log( "Iterated " + counter + " times" );
});
});
每次光标迭代时都会触发数据"事件,并且对象上的属性将显示流是完整的还是仍在迭代中,依此类推.
The "data" event is fired with each cursor iteration and properties on the object will show whether the stream is complete or still iterating and so on.
这篇关于Nodejs驱动程序支持哪些聚合游标方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!