不幸的是,目前不支持此功能。
您可以创建一个辅助函数,将其“转换”为一个
bson.document类似这样的结构值:
func toDoc(v interface{}) (doc *bson.document, err error) { data, err := bson.Marshal(v) if err != nil { return } err = bson.Unmarshal(data, &doc) return}然后可以这样使用:
partialUpdate := &NoteUpdate{ Title: "Some new title",}doc, err := toDoc(partialUpdate)// check errorres := c.FindoneAndUpdate( context.Background(), bson.Newdocument(bson.EC.String("_id", "some-note-id")), bson.Newdocument(bson.EC.Subdocument("$set", doc)),)希望
ElementConstructor.Interface()将来会有所改进,并允许直接传递结构值或指向结构值的指针。



