2 回答

TA贡献1871条经验 获得超13个赞
在转换查询时,使用映射和切片类型并查找类型来代替 mongo 驱动程序别名。bson.A{bson.M{...}, bson.M{...}}
[]bson.M{...}
bson.M{}
bson.A{}

TA贡献2003条经验 获得超2个赞
很抱歉回复晚了。我完全忘记了发布解决方案。就像@prasad_说的,我试图使用开关盒运算符,但使用.UpdateOne()
ctx := context.Background()
filter := bson.M{
"_id": ID,
}
back := bson.M{
"case": bson.M{
"$and": []bson.M{
{"$gt": []interface{}{"$section.gateBack", 0}},
{"$exists": []interface{}{"$section.gateBack", true}},
},
},
"then": time.Now().AddDate(0, 0, 1),
}
middle := bson.M{
"case": bson.M{
"$and": []bson.M{
{"$gt": []interface{}{"$section.gateMiddle", 0}},
{"$exists": []interface{}{"$section.gateMiddle", true}},
},
},
"then": time.Now().AddDate(0, 1, 0),
}
front := bson.M{
"case": bson.M{
"$and": []bson.M{
{"$gt": []interface{}{"$section.gateFront", 0}},
{"$exists": []interface{}{"$section.gateFront", true}},
{"$in": []interface{}{"$section.access", []string{"gate", "door"}}},
},
},
"then": time.Now().AddDate(1, 0, 0),
}
cases := bson.A{back, middle, front}
update := []bson.M{
{
"$set": bson.M{
"expiresAt": bson.M{
"$switch": bson.M{
"branches": cases,
"default": time.Now().AddDate(0, 0, 0),
},
},
},
},
}
res, err := coll.UpdateOne(
ctx,
filter,
update,
)
if err != nil {
return 0, err
}
- 2 回答
- 0 关注
- 117 浏览
添加回答
举报