我参与过 Go 和 MongoDb 项目。其中,我遇到了一个问题。我已经使用 $in 运算符将整数的动态切片 ([]int) 传递到查询中。每件事看起来都很好,但是当这个切片为空时,它会返回一个错误“$in 需要一个数组”。但是我将它用作搜索参数,并且如果我传递空白数组,则它与所有数组匹配。
注意:我使用的是 MongoDB shell 版本 v5.0.3
这是我的代码:
var searchedProfiles []int
searchFilter := bson.M{"customer.id": bson.M{"$in": searchedProfiles}}
newQry := []bson.M{
{"$lookup": bson.M{
"localField": "cid",
"from": "customers",
"foreignField": "_id",
"as": "customer"}},
{"$match": searchFilter},
}
如果有人有任何想法,请告诉我。谢谢!
蛊毒传说
相关分类