簡體   English   中英

Mongo使用空的objectid引用保存文檔-錯誤:JSON中的ObjectId無效

[英]Mongo save document with empty objectid reference - error: Invalid ObjectId in JSON

我正在使用連接到mongo的golang服務器。

我有以下參考結構:

type A struct {
    Id   bson.ObjectId    `bson:"_id" json:"id"`
    B    bson.ObjectId    `bson:"b,omitempty" json:"b,omitempty"`
}

事實是,B在A中不是強制性的,每當我嘗試保存A而沒有B時,我都會遇到錯誤:

"Invalid ObjectId in JSON: null"

我怎樣才能使此參考不是強制性的?

您可以嘗試:

type A struct {
    Id bson.ObjectId  `bson:"_id" json:"id"`
    B  *bson.ObjectId `bson:"b,omitempty" json:"b,omitempty"`
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM