[英]How to use mongocompact package in mongoclient connect options
Currently I already use NewRegistryBuilder()
in the mongo client options, Somehow i want to make use of NewRespectNilValuesRegistryBuilder()
from mongocompact so that I can set the nilvalues in the []models as empty array.目前我已经在 mongo 客户端选项中使用了 NewRegistryBuilder( NewRegistryBuilder()
,我想以某种方式使用 mongocompact 中的NewRespectNilValuesRegistryBuilder NewRespectNilValuesRegistryBuilder()
以便我可以将 [] 模型中的 nilvalues 设置为空数组。
Currently I have something like below, To map the old mgo behavior目前我有类似下面的内容,对于 map 旧的 mgo 行为
tM := reflect.TypeOf(bson.M{})
registry := bson.NewRegistryBuilder().RegisterTypeMapEntry(bsontype.EmbeddedDocument, tM).Build()
clientOpts := options.Client().ApplyURI(URI).SetAuth(info).SetRegistry(registry)
client, err := mongo.Connect(ctx, clientOpts)
Now somehow I want to incorporate and set the NewRespectNilValuesRegistryBuilder().Build()
to true in the same connect options, Not sure how to do that.现在我想以某种方式合并NewRespectNilValuesRegistryBuilder().Build()
并将其设置为 true 在相同的连接选项中,不知道该怎么做。 Also the other problem that i see is i'm not able to see the mongocompat registry file in my vendor directory我看到的另一个问题是我无法在我的供应商目录中看到 mongocompat 注册表文件
can i do something like this, SetRegistry()
two times?我可以做两次这样的SetRegistry()
吗? like below?像下面?
tM := reflect.TypeOf(bson.M{})
registry := bson.NewRegistryBuilder().RegisterTypeMapEntry(bsontype.EmbeddedDocument, tM).Build()
clientOpts := options.Client().ApplyURI(SOMEURI).SetAuth(info).SetRegistry(registry)
//not sure if the below line is of right usage ??
clientOpts.SetRegistry(NewRespectNilValuesRegistryBuilder().Build())
client, err := mongo.Connect(ctx, clientOpts)
can anyone help me with this?谁能帮我这个?
I found a solution to this one, All we need to do is我找到了解决这个问题的方法,我们需要做的就是
registry := mgocompat.NewRegistryBuilder().Build()
connectOpts := options.Client().ApplyURI(SOMEURI).SetAuth(info).SetRegistry(registry)
This will take care of both requirements, no need to say这样就可以兼顾这两个需求了,不用多说
tM := reflect.TypeOf(bson.M{})
Since that's a default behavior that's included in mgocompat
因为这是包含在mgocompat
中的默认行为
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.