gpt4 book ai didi

mongodb-go-driver/bson struct 到 bson.Document 编码

转载 作者:IT王子 更新时间:2023-10-29 01:40:45 26 4
gpt4 key购买 nike

我正在使用 https://github.com/mongodb/mongo-go-driver和目前正在尝试实现此类结构的部分更新

type NoteUpdate struct {
ID string `json:"id,omitempty" bson:"_id,omitempty"`
Title string `json:"title" bson:"title,omitempty"`
Content string `json:"content" bson:"content,omitempty"`
ChangedAt int64 `json:"changed_at" bson:"changed_at"`
}

例如,如果我有

noteUpdate := NoteUpdate{ Title: "New Title" }

然后我希望存储文档中唯一的“标题”字段会被更改。

我需要写一些类似的东西

collection.FindOneAndUpdate(context.Background(),
bson.NewDocument(bson.EC.String("_id", noteUpdate.ID)),
// I need to encode non-empty fields here
bson.NewDocument(bson.EC.SubDocument("$set", bson.NewDocument(...)))
)

问题是我不想用 bson.EC.String(...)bson.EC.Int64(.. .)。我尝试使用 bson.EC.InterfaceErr(...) 但出现错误

Cannot create element for type *models.NoteUpdate, try using bsoncodec.ConstructElementErr

很遗憾,bsoncodec中没有这个功能。我发现的唯一方法是创建包装器

type SetWrapper struct {
Set interface{} `bson:"$set,omitempty"`
}

像这样使用它

partialUpdate := &NoteUpdate{
ID: "some-note-id",
Title: "Some new title",
}
updateParam := SetWrapper{Set: partialUpdate}
collection.FindOneAndUpdate(
context.Background(),
bson.NewDocument(bson.EC.String("_id", noteUpdate.ID)),
updateParam,
)

它有效,但是否有可能使用 bson/bsoncodec 文档构建器实现相同的效果?

更新。我的问题的完整背景:我为 部分 更新“Note”文档(存储在 MongoDB 中)编写了 REST 端点。我现在拥有的代码:

var noteUpdate models.NoteUpdate
ctx.BindJSON(&noteUpdate)
//omit validation and errors handling
updateParams := services.SetWrapper{Set: noteUpdate}
res := collection.FindOneAndUpdate(
context.Background(),
bson.NewDocument(bson.EC.String("_id", noteUpdate.ID)),
updateParams,
findopt.OptReturnDocument(option.After),
)

我想要的代码

var noteUpdate models.NoteUpdate
ctx.BindJSON(&noteUpdate)
//omit validation and errors handling
res := collection.FindOneAndUpdate(
context.Background(),
bson.NewDocument(bson.EC.String("_id", noteUpdate.ID)),
bson.NewDocument(
//bsoncodec.ConstructElement doesn't exists
bsoncodec.ConstructElement("$set", &noteUpdate)),
),
findopt.OptReturnDocument(option.After),
)

想要的代码

var noteUpdate models.NoteUpdate
ctx.BindJSON(&noteUpdate)
//omit validation and errors handling
bsonNote := bson.NewDocument()
if noteUpdate.Title != "" {
bsonNote.Append(bson.EC.String("title", noteUpdate.Title))
}
if noteUpdate.Content != "" {
bsonNote.Append(bson.EC.String("content", noteUpdate.Content))
}
//..setting the rest of the fields...
res := collection.FindOneAndUpdate(
context.Background(),
bson.NewDocument(bson.EC.String("_id", noteUpdate.ID)),
bson.NewDocument(bson.EC.SubDocument("$set", bsonNote)),
findopt.OptReturnDocument(option.After),
)

所以,确切的问题是 - 有没有什么方法可以基于 bson 标签动态构建 *bson.Document(没有像我的 SetWrapper 这样的预定义包装器)?

最佳答案

很遗憾,目前不支持此功能。

您可以创建一个辅助函数,将结构值“转换”为 bson.D像这样:

func toDoc(v interface{}) (doc *bson.D, 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 error

res := c.FindOneAndUpdate(
context.Background(),
bson.NewDocument(bson.EC.String("_id", "some-note-id")),
bson.NewDocument(bson.EC.SubDocument("$set", doc)),
)

希望ElementConstructor.Interface()将来会改进并允许直接传递结构值或指向结构值的指针。

关于mongodb-go-driver/bson struct 到 bson.Document 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53110020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com