当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - mongo - how to access data returned by group reduce

问题描述:

I have a javascript file as

var x = new Mongo('127.0.0.1:27017');

var db = x.getDB('user');

var r = db.runCommand({"group" : {

"ns" : "track_user", "key" : {userId : 1}, "initial" : {

count:0 }, cond: {

uri: {

$regex: 'sm'

} }, "$reduce" : function(doc, prev) {

} } } }});

while(r.hasNext()){

rs = r.next();

}

I am not able to run through results returned by group reduce.

output of group reduce run via shell is something like

{

"retval" : [

{

"userId" : 0,

"created" : "2013-08-08 11:32:15",

"uri" : "abc.com",

"uri_set" : [

"xyz.com"

]

},...

],

"count" : 14,

"keys" : 6,

"ok" : 1

}

hasNext is giving error as

Sat Nov 23 00:53:40.351 JavaScript execution failed: TypeError: Object [object Object] has no method 'hasNext' at /var/www/admin/app/controllers/newjavascript1.js:L62

Please help asap.

网友答案:

The problem is that db.runCommand() does not return a cursor, so none of the cursor methods are going to work on it. You're returning a document with the results nested inside, so you're going to have to work with the data a different way.

You should be able to either use map or each, depending on what you're trying to do:

var mySet = r.retval;

mySet.forEach( function(doc) {
    console.log( doc );
});

Or, whatever you need it to do.

分享给朋友:
您可能感兴趣的文章:
随机阅读: