<p>In general, I need to define a struct to tell mgo, which fields I need, for example:</p>
<p><code>
type PrivacyInfo struct {
Phone string `bson:"phone"`
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
}
</code>
But, I have so many collections, which the name of phone number is different, such as "TelephoneNumber", "Number", "MobileNumber" and etc. I don't know which collection I will use until the program is running. So I hope I can input the collection name and the field name and then the program read the number. </p>
<p>But in golang, compare with Python's pymongo or even MongoDB's default shell, mgo is really a pot of shit!!! </p>
<hr/>**评论:**<br/><br/>TheMue: <pre><p>Maybe it's simply due to the data structure. Lists of tuples with type and number would be more flexible and better searchable. Blaming a database driver doesn't help. </p></pre>ligustah: <pre><p>I don't think this has anything to do with mgo. If you don't know the schema upfront you'll just have to use <code>bson.M{}</code> and then figure out the parts you need (there are libraries to help with that). That's the price you're paying to be schema-less.</p></pre>kingname: <pre><p>Schema can not work. Because it need declare a struct first and then can it transfer a map to struct. But before I input the name of a field, how can I declare a struct?</p></pre>ligustah: <pre><p>I think some of the other answers already provided good explanations on this.</p></pre>TheMue: <pre><p>The driver is intended to serialize data structures and use Mongo for persistency (top-down). You instead want to access a number of different structured documents (bottom-up).</p>
<p>It's like trying to hammer a nail with a screwdriver and then blame it that it doesn't work. But it's very good for screws where a hammer fails. </p></pre>kingname: <pre><p>Is there a "hammer" to solve my problem in Golang?</p></pre>TheMue: <pre><p>Most generic datatypes regarding marshaling of JSON are interface{}, []interface{}, or map[string]interface{}. These structures are nested then. These interfaces are statically typed then when doing the unmarshaling, but you have to work with type assertions if the may vary.</p>
<p>While JSON has this flexibility a static-typed language like Go is a bit more complex here. That's why it's so positive to know your data structures well. We do the same here with CouchDB. We simply serialize our types into the DB and later retrieve them, as deep nested structures.</p></pre>aboukirev: <pre><p>Correction, the data structure you working with is at fault.
All you have to do is use a map or <code>D</code>type from<code>bson</code> sub-package as a target result when working with <code>Query</code> and BSON will be unmarshaled/deserialized just fine into it. Then you can examine it to your heart's content.</p>
<p>The relevant documentation is at <a href="http://godoc.org/gopkg.in/mgo.v2/bson#Unmarshal" rel="nofollow">http://godoc.org/gopkg.in/mgo.v2/bson#Unmarshal</a></p></pre>kingname: <pre><p>Thanks for the document, I will read it carefully.</p></pre>smartsam69: <pre><p>In my experience, a programmer that tries to blindly blame the tools is usually the one at fault</p></pre>tsdtsdtsd: <pre><p>Maybe check out the official mongo-tools sources at <a href="https://github.com/mongodb/mongo-tools" rel="nofollow">https://github.com/mongodb/mongo-tools</a></p>
<p>Most (or all?) are written in Go and use mgo, without the structs.</p>
<p>Still, for me the structs are great. Clean code enforcing, strictly typed awesomeness. I don't get how someone could hate that so much.</p></pre>kingname: <pre><p>OK, I will read it. Thanks a lot.</p></pre>Patchthesock: <pre><p>What pot of shit did you use to compare? Why not use a better maybe more agnostic data structure? As TheMue has said blaming a database driver isn't going to help.</p></pre>kingname: <pre><p>MongoDB‘s shell is 10000 times better than mgo, and let alone pymongo</p></pre>carsncode: <pre><p>You're comparing apples and Bolivia. The shell isn't a DB SDK, that's a pointless comparison. Python and Go are very different languages, so pymongo is a slightly better but still inapplicable comparison.</p></pre>kingname: <pre><p>A man who write a database driver to wrap the origin api is aim to make it more usable. But mgo back the car, it makes MongoDB so hard to use.</p></pre>kingname: <pre><p>Any one who is familiar with pymongo or mongoengine or even MongoDB's default shell will say, mgo is a pot of shit!. </p></pre>skidooer: <pre><p>...so don't use it?</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传