I am very new to Go, and fairly new to development in general.
As a learning experience I am rewriting the backend of a webapp I previously wrote in nodejs.
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.
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.
I have in my results via a query a _id of:
353533666262333366653837303837633239396565623463
and in the actual DBthe _id is:
553fbb33fe87087c299eeb4c
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 ....
I have tried getting the _id via :
err := MongoDB.DB("taskerDB").C("tasks").Find(bson.M{"assignedUser":"unassigned"}).All(&updatedmsg)
for _, update := range updatedmsg {
msg["Id"] = update.Id.Hex()
... ... ...
and msg["Id"] = string(bson.ObjectId(update.Id.Hex())) ... ... ...
which i read on some blog posts, but either way I dont get the Id as 553fbb33fe87087c299eeb4c...
Any help would be greatly appreciated.
评论:
sharptierce:
klauspost:to get the hex value just use the method: https://godoc.org/labix.org/v2/mgo/bson#ObjectId.Hex
I agree that its confusing. The problem here is how you print the result.
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.
Try printing the value with fmt.Printf("%s", id).
chreestopher2:An ObjectID is actually stored as 16 raw bytes. These 16 bytes are then usually converted to hex, but it seems like something has done it twice.
Here is how to decode it: http://play.golang.org/p/jTATz4vWg0
What you are seeing is: '35' means the hex value 0x35, which is the ascii value for '5', and so on.
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.