插入时mgo NewObjectId损坏 [英] mgo NewObjectId corrupt on insert
问题描述
obId:= bson.NewObjectId()$ b如果我为mgo中的文档生成新的对象ID, $ b
然后插入它,它以mongo结尾(通过cli查看)为
_ id:U ` \\\@ \rU\\\ \\\ \\\
当它应该是
_ id:ObjectId(559a47643d9827f0d9405420)
如果尝试更新现有的/ b
obId:= bson.ObjectIdHex(stringId)
它仍然被序列化为损坏的格式。
我尝试的结构插入如下所示:
type MyStruct struct {
Id bson.ObjectId`bson:_ id,omitempty json:id`
...
}
代表U ` \\\@ \rU\\\ \\\ \\\
清楚地表明ObjectId以字符串的形式发送到数据库,而不是一个正确类型的对象ID。之前的每个这种情况都是应用程序端的代码路径,因为这样错误地传递了字符串。我建议调查在该集合中插入对象的每个代码路径,并且如果您没有发现将它作为实际字符串发送的情况,则尝试创建一个复制器并将其上报给mgo驱动程序。
更新:根据以下您的评论,问题是由于应用程序的某些部分正在使用 ObjectId
从一个不是在与数据库通信期间实际使用的包中输入。这具有上述效果:来自错误包的 ObjectId
类型只是一个普通字符串,只要正确的 bson
软件包。
If I generate a new object id for a document in mgo:
obId := bson.NewObjectId()
and then insert it, it ends up in mongo (looking via the cli) as
"_id" : "U�`�\u0006@�\rU\u0000\u0000\u0001"
When it should be
"_id" : ObjectId("559a47643d9827f0d9405420")
Same goes if I try and update an existing document where I generate the id by
obId := bson.ObjectIdHex(stringId)
It still gets serialized to the corrupted format.
My struct which I'm trying to insert looks like this:
type MyStruct struct {
Id bson.ObjectId `bson:"_id,omitempty" json:"id"`
...
}
The representation "U�`�\u0006@�\rU\u0000\u0000\u0001"
is clearly indicating that an ObjectId got sent to the database as a string rather than as a properly typed object id. Every such case before was a code path in the application side delivering the string explicitly as such by mistake. I recommend investigating every code path that inserts objects in that collection, and if you can find no case that is sending it as an actual string, then try to create a reproducer and report it upstream to the mgo driver.
Update: Per your comment below, the issue is being caused because some part of the application is using an ObjectId
type from a package that is not the one actually used during communication with the database. This has the effect described above: the ObjectId
type coming from the wrong package is just a normal string, as far as the correct bson
package is concerned.
这篇关于插入时mgo NewObjectId损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!