MongoDB GoLang functions

xuanbao · 2018-04-23 21:30:12 · 742 次点击    
这是一个分享于 2018-04-23 21:30:12 的资源,其中的信息可能已经有所发展或是发生改变。

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_:

Well, what have you tried?

barryzxb:

you need read the documentation of MangoAPI in details.

circuitously:

I wouldn’t use mgo, it’s been abandoned.

MongoDB have been working on their own driver, I would suggest giving it a go.

https://github.com/mongodb/mongo-go-driver

thajunk:

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

fmpwizard:

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


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

742 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传