<p>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.</p>
<hr/>**评论:**<br/><br/>jayposs: <pre><p>Here's a basic example:<br/>
<a href="https://play.golang.org/p/MrI29X_Djp" rel="nofollow">https://play.golang.org/p/MrI29X_Djp</a> </p>
<p>You will need to study both the MongoDB and MGO docs to really learn how to use the more sophisticated features. </p>
<p>The type bson.M is an alias for map[string]interface{}</p></pre>erdeicodrut: <pre><p>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?</p></pre>jayposs: <pre><pre><code>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"}}
</code></pre>
<p>Hopefully that works.<br/>
I usually set the key value using: doc.Id = bson.NewObjectId().Hex()<br/>
That way I know what the key value is up front and it is a simple string that is easy to work with.</p></pre>erdeicodrut: <pre><p>Thank you</p></pre>PaluMacil: <pre><p>I've only used Mongo in C#, but <a href="http://goinbigdata.com/how-to-build-microservice-with-mongodb-in-golang/" rel="nofollow">goinbigdata has an example</a> 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.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传