Golang mgo MongoDB bson.ObjectId 非 UTF-8 错误

huangapple go评论82阅读模式
英文:

Golang mgo MongoDB bson.ObjectId non utf-8 error

问题

我正在使用Mongo和mgo驱动在我的Mac上用Go语言进行开发。

在我的Mac上一切都很正常。但是当我的朋友在他的Windows机器上使用相同的代码库时,我们得到了一些奇怪的非UTF-8编码的bson.ObjectIds。

这是mongolab.com(一个托管的Mongo服务器)的截图:

Golang mgo MongoDB bson.ObjectId 非 UTF-8 错误

我的代码简单地使用了以下内容:

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

有人遇到过这个问题吗?有人知道如何处理吗?

编辑:在这个代码库中使用的所有bson函数如下:

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

谢谢。

英文:

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)

Golang mgo MongoDB bson.ObjectId 非 UTF-8 错误

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.

答案1

得分: 1

在截图中,Unicode替换字符表明该应用程序将对象ID视为UTF-8编码的文本。对象ID包含的是二进制数据,而不是UTF-8编码的文本。

使用Hex方法将对象ID转换为可读的文本。

使用ObjectIdHex将十六进制字符串表示转换回对象ID。请注意,要保护对ObjectIdHex的调用,可以使用IsObjectIdHex

英文:

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.

huangapple
  • 本文由 发表于 2015年3月16日 01:13:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/29063656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定