我有一个包含以下文档的集合:
{ID: xxx, firstCreateDate: xxx, isActive: false}
Run Code Online (Sandbox Code Playgroud)
isActive = false,然后更新isActive为 true,如果之前未设置,也更新firstCreateDate为当前日期。我不确定如何有条件地更新提交的firstCreateDate。目前无法进行有条件更新。得到
firstCreateDate = {$cond:[{"firstCreateDate":{"$exists":false}}, "$firstCreateDate", newdata]}ID字段生成新字段internalID。我尝试使用UpdateMany(),但似乎无法使用您自己的函数获取该值并计算新值。我正在考虑使用Find()查找所有文档,然后对其进行解码,然后根据id一一更新文档。对此有更好的解决方案吗?这就是我现在所拥有的:
filter := bson.M{"isActive": false}
update := bson.M{"set": bson.M{"isActive": true, "firstCreateDate": bson.M{
"$cond": bson.A{
bson.M{"firstCreateDate", bson.M{"$exists", false}},
"$firstCreateDate",
newDate,
}
}}}
collection.UpdateByID(
context.Background(),
id,
update,
)
Run Code Online (Sandbox Code Playgroud)
我正在使用mongo-driver lib,任何人都可以提供帮助
我在我的 go 项目中使用 grpc。下面是代码:
示例.proto:
syntax = "proto3";
message Example {
string message = 1;
google.protobuf.Any details = 2;
}
Run Code Online (Sandbox Code Playgroud)
main.go
func logMessage (m string, d interface{}) {
message := & example.message{
message: m,
details: ??
}
log(&message)
}
Run Code Online (Sandbox Code Playgroud)
但我不确定如何处理 details(interface{}) 字段。我知道我可以使用任何类型的接口,但不确定如何在这里使用它。任何人都可以帮忙吗?谢谢