Abe Miessler
Abe Miessler

Reputation: 85056

How to print results of script?

I have a pretty basic script file that I would like to run and have it's output spit out. The contents are below:

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

printjson(data);

I've tried doing load('/path/to/script.js') but it outputs a giant blob of information like so:

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

How can I only display the results of the script and not all the other junk?

Upvotes: 5

Views: 3901

Answers (1)

Daniel W.
Daniel W.

Reputation: 32290

You are getting back an Iterator or Cursor.

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

data.forEach(printjson);

Or:

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

Upvotes: 7

Related Questions