MGO

polaris · 2017-10-24 00:00:08 · 957 次点击    
这是一个分享于 2017-10-24 00:00:08 的资源,其中的信息可能已经有所发展或是发生改变。

Can someone post a commented code of a basic use of MongoDB. initialization and queries. If there is smth else to know I'm all ears.


评论:

jayposs:

Here's a basic example:
https://play.golang.org/p/MrI29X_Djp

You will need to study both the MongoDB and MGO docs to really learn how to use the more sophisticated features.

The type bson.M is an alias for map[string]interface{}

erdeicodrut:

I'm really thankfull. it seems to be working. If im not asking for too much, can you give an example of inserting data into the db?

jayposs:
aPerson := person_doc{LastName:"bower", Zip:"71234"} // _id is auto loaded if not set
err = collection.Insert(aPerson)
if mgo.IsDup(err) {
// got a duplicate key error, shouldn't happen with auto loaded id }
// update
err = collection.UpdateId(docId, bson.M{"$set": bson.M{"last_name":"bauer"}}

Hopefully that works.
I usually set the key value using: doc.Id = bson.NewObjectId().Hex()
That way I know what the key value is up front and it is a simple string that is easy to work with.

erdeicodrut:

Thank you

PaluMacil:

I've only used Mongo in C#, but goinbigdata has an example that looks pretty good. It uses Goji, but if you wanted a different router, that part shouldn't be hard to swap out since it is composable with net/http.


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

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