<p>Hi everyone!
I have basic code to delete duplicates from collection in MongoDB</p>
<pre><code>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}});
})
</code></pre>
<p>How all of this execute using library <a href="https://github.com/go-mgo/mgo/tree/v2" rel="nofollow">MGO</a> ?</p>
<p>I’ve see examples with Pipe (for sorting and grouping) but how use “forEach” function in MGO</p>
<p>Did someone execute something like this?</p>
<p>Thanks!</p>
<hr/>**评论:**<br/><br/>papers_: <pre><p>Well, what have you tried?</p></pre>barryzxb: <pre><p>you need read the documentation of MangoAPI in details.</p></pre>circuitously: <pre><p>I wouldn’t use mgo, it’s been abandoned.</p>
<p>MongoDB have been working on their own driver, I would suggest giving it a go.</p>
<p><a href="https://github.com/mongodb/mongo-go-driver" rel="nofollow">https://github.com/mongodb/mongo-go-driver</a></p></pre>thajunk: <pre><p>Unfortunately that official driver is still lacking alot of features. Would be ideal to move to in thr future however.</p>
<p>Until then,</p>
<p>github.com/globalsign/mgo</p>
<p>Seem to be the most popular fork of mgo</p></pre>fmpwizard: <pre><p>after you create a Pipe, you then use </p>
<p><a href="https://godoc.org/labix.org/v2/mgo#Pipe.Iter" rel="nofollow">https://godoc.org/labix.org/v2/mgo#Pipe.Iter</a></p>
<p>and use regular go code to walk the iter and delete the documents you want</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传