<p>I am very new to Go, and fairly new to development in general.</p>
<p>As a learning experience I am rewriting the backend of a webapp I previously wrote in nodejs.</p>
<p>I am having a problem, and im sure its something simple that I am overlooking, but ive searched and cannot for the life of me figure out what im doing wrong.</p>
<p>I have some documents that I am retrieving from a mongodb and I can successfully get docs from a query if i look for any particular property except for the _id property. I have noticed when I get results based on a successful query, the _id does not match that which is in the database for that particular document.</p>
<p>I have in my results via a query a _id of:</p>
<pre><code>353533666262333366653837303837633239396565623463
</code></pre>
<p>and in the actual DBthe _id is:</p>
<pre><code>553fbb33fe87087c299eeb4c
</code></pre>
<p>I am assuming in the DB it is hex and what im getting is decimal? but ive tried to check that quickly with an online converter, but the numbers dont seem to match up .... </p>
<p>I have tried getting the _id via :</p>
<pre><code>err := MongoDB.DB("taskerDB").C("tasks").Find(bson.M{"assignedUser":"unassigned"}).All(&updatedmsg)
for _, update := range updatedmsg {
msg["Id"] = update.Id.Hex()
... ... ...
</code></pre>
<p>and
msg["Id"] = string(bson.ObjectId(update.Id.Hex()))
... ... ...</p>
<p>which i read on some blog posts, but either way I dont get the Id as 553fbb33fe87087c299eeb4c...</p>
<p>Any help would be greatly appreciated.</p>
<hr/>**评论:**<br/><br/>sharptierce: <pre><p>to get the hex value just use the method: <a href="https://godoc.org/labix.org/v2/mgo/bson#ObjectId.Hex" rel="nofollow">https://godoc.org/labix.org/v2/mgo/bson#ObjectId.Hex</a></p>
<p>I agree that its confusing. The problem here is how you print the result.</p>
<p>ObjectID implements the fmt.Stringer() interface which would give you a nice output. But you cast the type to string, so it loses all informations about its type. </p>
<p>Try printing the value with fmt.Printf("%s", id).</p></pre>klauspost: <pre><p>An ObjectID is actually stored as 16 raw bytes. These 16 bytes are then <em>usually</em> converted to hex, but it seems like something has done it twice.</p>
<p>Here is how to decode it: <a href="http://play.golang.org/p/jTATz4vWg0" rel="nofollow">http://play.golang.org/p/jTATz4vWg0</a></p>
<p>What you are seeing is: '35' means the hex value 0x35, which is the ascii value for '5', and so on.</p></pre>chreestopher2: <pre><p>thank you for this, however I am still unable to find the root of my problem... im taking a break for tonight and will hack at it some more tomorrow, if i continue to have trouble i will post a more full gist and see if anyone can spot what dumb mistake im making.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传