自定义 encoder/decoder 用于插入或获取 mongo-驱动程序的文档

custom encoder/decoder for insert or geting documents for mongo-driver

我读过这个friendly article关于使用官方 go mongo 驱动程序编码和解码自定义对象。

有一个很好的示例如何将它们编组为扩展 json 格式 (bson.MarshalExtJSONWithRegistry)。但我想知道如何将此文档放入 InserOne() 集合中(然后从中获取)。看看这个代码:

// myReg - variable created according to linked article in question.

// WithRegistry do **not** exist in mongo-driver lib is part of pseudocode
mongoCollection := client.Database("db").Collection("coll").WithRegistry(myReg)
// Now InserOne() honor myReg (type *bsoncodec.Registry) when serialize `val` and puting it into mongodb
mongoCollection.InsertOne(context.TODO(), val)

我已经浏览了 API 文档,我发现有 Marshaler and Unmarshaler 接口,但是通过注册表方式,我可以在不同的集合上以不同的方式(反)序列化相同的类型(例如,从旧格式迁移到新格式时)。

所以问题是如何将 *bsoncodec.Registry 与集合函数(如 InserOneUpdateOneFindOne 等)一起使用,如果不是,最惯用的方法是什么实现我的目标(自定义(反)序列化)。

Database.Collection() method has "optional" options.CollectionOptions parameter which does have option to set the bsoncodec.Registry。如果您使用通过注册表配置的选项获取集合,则该注册表将用于对该集合执行的所有操作。

这样使用:

opts := options.Collection().SetRegistry(myReg)
c := client.Database("db").Collection("coll", opts)

引用我的相关回答:

Registries can be set / applied at multiple levels, even to a whole mongo.Client, or to a mongo.Database or just to a mongo.Collection, when acquiring them, as part of their options, e.g. options.ClientOptions.SetRegistry().

因此,当您不从旧格式迁移到新格式时,您可以在“客户端”级别设置注册表并“完成它”。只要驱动程序处理您注册的自定义类型的值,就会应用您的注册表和自定义编码器/解码器。