2 回答

TA貢獻(xiàn)2012條經(jīng)驗(yàn) 獲得超12個贊
MongoDB 驅(qū)動程序僅使用bson
標(biāo)簽。json
標(biāo)簽僅用于encoding/json
包(或其他處理 JSON 編組/解組的第 3 方包)。
您不需要指定和使用bson
標(biāo)簽,在這種情況下,驅(qū)動程序在編碼結(jié)構(gòu)值時通常只使用小寫的字段名稱。bson
但是,當(dāng)您需要不同的名稱時,標(biāo)簽是必需的。
bson
即使您想使用小寫的字段名稱,指定標(biāo)簽也是一個好習(xí)慣,因?yàn)橛袝r您可能需要重命名結(jié)構(gòu)字段,這會導(dǎo)致麻煩和不一致。如果您指定bson
標(biāo)簽,那么將來是否重命名字段都沒有關(guān)系,它們?nèi)詫⒈痪幗M到相同的屬性中,并且解組它們將繼續(xù)起作用。

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個贊
bson 標(biāo)簽也可以包含改變默認(rèn)封送行為的標(biāo)志:
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.
- 2 回答
- 0 關(guān)注
- 238 瀏覽
添加回答
舉報