1

I'm developing in go on my Mac using mongo and mgo driver.

Everything works great on my Mac. When my friend works on the same codebase from his windows machine, we get these weird non utf-8 bson.ObjectIds.

Here is a screenshot from mongolab.com (a hosted mongo server)

enter image description here

My code simply uses:

thing.Id = bson.NewObjectId() 
thing.eventId = event.Id

Has anyone had this issue? Does anyone know how to deal with that

Edit: All bson functions used in this codebase are:

thing.Id = bson.NewObjectId()
thing.Id = bson.ObjectIdHex(id)
idString = thing.Id.Hex()

Thanks.

collinglass
  • 800
  • 4
  • 11
  • 31
  • 1
    Please show the type declarations for the _id and eventId fields. – Charlie Tumahai Mar 15 '15 at 20:50
  • 1
    How are you writing the `thing.eventId` field to the database? The `eventId` field is ignored by the BSON encoder because the field is not exported. –  Mar 15 '15 at 21:44
  • did you get this problem sorted? I think I'm having the same issue, see http://stackoverflow.com/questions/31244438/mgo-newobjectid-corrupt-on-insert – byrnedo Jul 06 '15 at 11:37
  • It was an outdated version of mgo on my buddies windows system. – collinglass Jul 06 '15 at 12:11

1 Answers1

1

The Unicode replacement character in the screenshot suggests that the application is treating object ids as UTF-8 encoded text. Object ids contain binary data, not UTF-8 encoded text.

Use the Hex method to convert an object id to human readable text.

Use ObjectIdHex to convert the hex string representation back to an object id. Be careful to protect the call to ObjectIdHex with IsObjectIdHex.

Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242
  • Maybe utf-8 isn't my issue. When I use my mac, the above snapshot would show all human readable characters. When the objects are created/used on the windows machine we get these weird characters. And this is on mongolab website. – collinglass Mar 15 '15 at 17:43
  • It's pretty obvious from [the documentation](http://docs.mongodb.org/manual/reference/object-id/) that they are using Go strings as just a collection of bytes and so I'd expect "interesting" output if trying to print it as-is (e.g. without %x or %q formatting). That's perfectly [legal in Go](http://blog.golang.org/strings) (even though normally you'd see `[]byte` used for this, the advantage of using `string` is that it's read-only). – Dave C Mar 15 '15 at 19:13
  • If I was them and I wanted to use an underlying string type I'd have made a custom `type ObjectIDType string` (but with a better name) to make it clearer that it's not a plain string. – Dave C Mar 15 '15 at 19:15
  • @DaveC They [define a custom type named ObjectId](http://godoc.org/gopkg.in/mgo.v2/bson#ObjectId). – Charlie Tumahai Mar 15 '15 at 19:20
  • @ThunderCat d'oh, I read over that too quickly, ignore my second comment :^) – Dave C Mar 15 '15 at 19:23