MGO

polaris · · 793 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Can someone post a commented code of a basic use of MongoDB. initialization and queries. If there is smth else to know I&#39;m all ears.</p> <hr/>**评论:**<br/><br/>jayposs: <pre><p>Here&#39;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&#39;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:&#34;bower&#34;, Zip:&#34;71234&#34;} // _id is auto loaded if not set err = collection.Insert(aPerson) if mgo.IsDup(err) { // got a duplicate key error, shouldn&#39;t happen with auto loaded id } // update err = collection.UpdateId(docId, bson.M{&#34;$set&#34;: bson.M{&#34;last_name&#34;:&#34;bauer&#34;}} </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&#39;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&#39;t be hard to swap out since it is composable with net/http.</p></pre>

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

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