Posts

Showing posts from May, 2015

MongoDB aggregate: Cursor example

Sometime necessary to access the MongoDb cursor and process the cursor data one by one. I had the similar situation var cur = db.users.aggregate( {$group : { _id : {un:{userName: '$userName'}}, count : {$sum : 1} }}, {$match: { count : {$gt: 1} }} ) cur['result'].forEach(function(doc){ var users = db.users.find({userName:doc._id.un.userName}); users.forEach(function(doc){ if (doc.status == 'archived'){ print(doc.userName)} }); }); as shown in the above, I create the cursor with the aggregation of all the users who’s user name is occurred more than once. Then that cur is iterated (using forEach function) and execute another database find to find the users whose status is ‘archived’ and only print them.