如何打印脚本的结果?

时间:2014-12-24 17:48:28

标签: mongodb

我有一个非常基本的脚本文件,我想运行并让它的输出吐出来。内容如下:

var data = db.participants.aggregate(
    {$match: {survey: ObjectId("548f9d0329ce94df22731668")}},
    {$group:{
            _id: "answers.question",
            totals: {$sum: 1}
        }
    }
);

printjson(data);

我已经尝试过load('/path/to/script.js'),但它输出了一大堆信息,如下:

    "_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 )

我怎样才能显示脚本的结果而不是所有其他垃圾?

1 个答案:

答案 0 :(得分:2)

您将返回IteratorCursor

http://docs.mongodb.org/manual/tutorial/iterate-a-cursor/

data.forEach(printjson);

或者:

while (data.hasNext()) {
   printjson(data.next());
}