我有一个非常基本的脚本文件,我想运行,并让它的输出吐出。内容如下:
var data = db.participants.aggregate(
{$match: {survey: ObjectId("548f9d0329ce94df22731668")}},
{$group:{
_id: "answers.question",
totals: {$sum: 1}
}
}
);
printjson(data);
我试过这样做,但它输出了一大团信息,比如:
"_firstBatch" : [
{
"_id" : "answers.question",
"totals" : 18566
}
],
"_cursor" : {
"next" : function next() { [native code] },
"hasNext" : function hasNext() { [native code] },
"objsLeftInBatch" : function objsLeftInBatch() { [native code] },
"readOnly" : function readOnly() { [native code] }
},
"hasNext" : function () {
return this._firstBatch.length || this._cursor.hasNext();
},
"next" : function () {
if (this._firstBatch.length) {
// $err wouldn't be in _firstBatch since ok was true.
return this._firstBatch.pop();
}
else {
var ret = this._cursor.next();
if ( ret.$err )
我如何才能只显示脚本的结果而不显示所有其他垃圾?
最佳答案
您将返回Iterator
或Cursor
。
http://docs.mongodb.org/manual/tutorial/iterate-a-cursor/
data.forEach(printjson);
或:
while (data.hasNext()) {
printjson(data.next());
}