doupai5450 2015-07-06 11:16
浏览 21
已采纳

mgo NewObjectId在插入时损坏

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@�U\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"`
    ...
}
  • 写回答

1条回答 默认 最新

  • douershuang7356 2015-07-06 14:13
    关注

    The representation "U�`�\u0006@�U\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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题