与MongoDB go客户端一起使用时需要bson结构标记

与MongoDB go客户端一起使用时需要bson结构标记,mongodb,go,bson,mongo-go,Mongodb,Go,Bson,Mongo Go,我正在观看一个关于如何创建使用MongoDB实现持久化的Go-restful API的教程 讲师在他的模型结构中同时使用json和bson标记,类似于 type NoteUpdate struct { ID string `json:"id,omitempty" bson:"_id,omitempty"` Title string `json:"title" bson:"title,omitempty"` Content string `json

我正在观看一个关于如何创建使用MongoDB实现持久化的Go-restful API的教程

讲师在他的模型结构中同时使用json和bson标记,类似于

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"`
}

然而,官方的围棋司机并没有这样做

事实上,这里根本没有使用结构标记

使用bson标签的目的/用途是什么

我想到的一件事是,如果你想创建自定义mongo_id字段,那么应该声明一个带有该结构字段的显式bson映射


bson标记还有其他附加值吗?

MongoDB驱动程序只使用bson标记。json标记仅用于encoding/json包或处理json封送/解封送的其他第三方包

不需要指定和使用bson标记,在这种情况下,驱动程序在编码结构值时通常只使用小写字段名。但是,当您需要其他名称时,需要bson标记


即使您想使用小写字段名,指定bson标记也是一种很好的做法,因为有时可能需要重命名结构字段,这会导致麻烦和不一致。如果您指定bson标记,将来是否重命名字段无关紧要,它们仍将被编组到相同的属性中,并且对它们进行解编组将继续工作。

MongoDB驱动程序仅使用bson标记。json标记仅用于encoding/json包或处理json封送/解封送的其他第三方包

不需要指定和使用bson标记,在这种情况下,驱动程序在编码结构值时通常只使用小写字段名。但是,当您需要其他名称时,需要bson标记


即使您想使用小写字段名,指定bson标记也是一种很好的做法,因为有时可能需要重命名结构字段,这会导致麻烦和不一致。如果指定bson标记,将来是否重命名字段无关紧要,它们仍将编组到相同的属性中,并且对它们进行解编组将继续工作。

bson标记还可以包含更改默认编组行为的内容:

OmitEmpty  Only include the field if it's not set to the zero value for the type or to
           empty slices or maps.

MinSize    Marshal an integer of a type larger than 32 bits value as an int32, if that's
           feasible while preserving the numeric value.

Truncate   When unmarshaling a BSON double, it is permitted to lose precision to fit within
           a float32.

Inline     Inline the field, which must be a struct or a map, causing all of its fields
           or keys to be processed as if they were part of the outer struct. For maps,
           keys must not conflict with the bson keys of other struct fields.

Skip       This struct field should be skipped. This is usually denoted by parsing a "-"
           for the name.

bson标记还可以包含更改默认封送处理行为的命令:

OmitEmpty  Only include the field if it's not set to the zero value for the type or to
           empty slices or maps.

MinSize    Marshal an integer of a type larger than 32 bits value as an int32, if that's
           feasible while preserving the numeric value.

Truncate   When unmarshaling a BSON double, it is permitted to lose precision to fit within
           a float32.

Inline     Inline the field, which must be a struct or a map, causing all of its fields
           or keys to be processed as if they were part of the outer struct. For maps,
           keys must not conflict with the bson keys of other struct fields.

Skip       This struct field should be skipped. This is usually denoted by parsing a "-"
           for the name.

如果需要更改结构的字段名,是否可以使用成功地取代bson标记?我问这个问题是因为我想使用生成的protobuf结构,不想在定制标签时遇到麻烦。我发现,它似乎可以解决所有的问题。乍一看,你的观点是什么?@YehudaMakarov使用自定义编解码器或实现自定义解组逻辑,你可以做任何你想做的事情。使用bson标记只是默认编解码器的默认行为。如果需要更改结构的字段名,使用是否可能成功地取代bson标记?我问这个问题是因为我想使用生成的protobuf结构,不想在定制标签时遇到麻烦。我发现,它似乎可以解决所有的问题。乍一看,你的观点是什么?@YehudaMakarov使用自定义编解码器或实现自定义解组逻辑,你可以做任何你想做的事情。使用bson标记只是默认编解码器的默认行为。