简体   繁体   中英

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

I'm working on golang server, connected to mongo.

I have a the following reference structure:

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

Thing is, B is not mandatory in A, and when ever I try to save A without B i'm getting an error:

"Invalid ObjectId in JSON: null"

How can I have this reference be no mandatory?

Can you try with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM