Hi everyone! I have basic code to delete duplicates from collection in MongoDB
db.myCollection.aggregate([
{"$group":
{
"_id": {"key": "$key"},
"dups": {"$push": "$_id"},
"count": { "$sum": 1 }
}},
{
"$match": {"count":{"$gt":1}}
}]).forEach(function(doc){
doc.dups.shift();
db.myCollection.remove({"_id" : {"$in": doc.dups}});
})
How all of this execute using library MGO ?
I’ve see examples with Pipe (for sorting and grouping) but how use “forEach” function in MGO
Did someone execute something like this?
Thanks!
评论:
papers_:
barryzxb:Well, what have you tried?
circuitously:you need read the documentation of MangoAPI in details.
thajunk:I wouldn’t use mgo, it’s been abandoned.
MongoDB have been working on their own driver, I would suggest giving it a go.
fmpwizard:Unfortunately that official driver is still lacking alot of features. Would be ideal to move to in thr future however.
Until then,
github.com/globalsign/mgo
Seem to be the most popular fork of mgo
after you create a Pipe, you then use
https://godoc.org/labix.org/v2/mgo#Pipe.Iter
and use regular go code to walk the iter and delete the documents you want
