如何在golang中为嵌套文档设置ObjectId?

Model:

type Vehicle   struct {
    Id          bson.ObjectId `bson:"_vid,omitempty"`
    Brand           string
    Category        string  
    CategorySubject     string
    MakeYear        string
    RegistrationNumber  string
    Model           string
    Price           string
}
func (this *Vehicle)AddToDB(emailId1 string)  {
    sess, db := GetDatabase()
    defer sess.Close()
    c := db.C("user")
    //newId :=Vehicle{}
    /*colQuerier := bson.M{"email": person.Email}
    change := bson.M{"$set": bson.M{"profile" : imgName}}
    err = c.Update(colQuerier, change)*/
    colQuerier := bson.M{"email": emailId1}
    change := bson.M{"$push": bson.M{"myvehicle" : &this}}
    err := c.Update(colQuerier, change)
    if err != nil {
        fmt.Println("not inserted") 
    }
}

The value of Id of Vehicle is empty when it's pushed into myvehicle field.

How do I set a value for the nested Vehicle's id ?

MongoDB ObjectId is auto inserted for document (not sub-document) that does not specify _id field. This is to uniquely identify the document.

In your case above, if you are inserting a nested object (sub-document) the Id field would not be auto-inserted by MongoDB.

You can however create an ObjectId (unique identifier) for the newly pushed Vehicle document. For example:

new_object_id := bson.NewObjectId()

See also NewObjectId