Iterate over Aggregates

617 views
Skip to first unread message

David Kerr

unread,
Nov 12, 2013, 4:52:54 PM11/12/13
to mongod...@googlegroups.com
I've got duplicate records in my database and I'm trying to get rid of them.

I'm able to find them via this aggregation:

db.users.aggregate(
       
{ $group : { _id: "$email", total: { $sum: 1} } },
       
{ $match : { total : { $gte : 2 } } } );



However it seems that db.users.aggregate doesn't return a cursor so i cannot do cursor.hasNext().
Nor does it return an array so that i can use forEach().

I end up with: Object [object Object] has no method 'hasNext'

Any suggestions on how to iterate over this data?


Asya Kamsky

unread,
Nov 12, 2013, 10:48:33 PM11/12/13
to mongodb-user
There is a shortcut way to get rid of duplicates...   You can use ensureIndex({email:1}, {unique:true, dropDups:true}).

I.e. create a unique index specifying that duplicate holding documents should be dropped.   

Asya



--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

David Kerr

unread,
Nov 12, 2013, 11:50:29 PM11/12/13
to mongod...@googlegroups.com
Yeah, I know about dropDups but unfortunately for me there is one specific record in these dups that is "correct" 
so I can't let Mongo decide which to drop.

Justin Lee

unread,
Nov 13, 2013, 4:11:55 AM11/13/13
to mongod...@googlegroups.com

Asya Kamsky

unread,
Nov 13, 2013, 4:12:36 AM11/13/13
to mongodb-user
In that case, assign the document returned from aggregate to a variable.

var x=db.collection.aggregate(etc)

Now you can iterate via 
x.results.forEach( etc)

Asya

David Kerr

unread,
Nov 13, 2013, 5:12:24 PM11/13/13
to mongod...@googlegroups.com
Thanks Justin and Asya,

I wasn't able to get x.results.forEach( < stuff > to work in javascript
but I got something similar working in python.

Looking forward to cursors in 2.6!
Reply all
Reply to author
Forward
0 new messages