2 回答

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超13個贊
在轉(zhuǎn)換查詢時,使用映射和切片類型并查找類型來代替 mongo 驅(qū)動程序別名。bson.A{bson.M{...}, bson.M{...}}
[]bson.M{...}
bson.M{}
bson.A{}

TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個贊
很抱歉回復(fù)晚了。我完全忘記了發(fā)布解決方案。就像@prasad_說的,我試圖使用開關(guān)盒運(yùn)算符,但使用.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 關(guān)注
- 104 瀏覽
添加回答
舉報