$或在$内部,并且在$filter条件下不起作用



我正在使用golang和mongodb,mgo集合

我的mongodb集合是

**department**
{
dept_id:1,
dept_name:'CSE',
dept_overview:'overview'
}
................
**employee**
{
emp_id:1,
emp_name:'abc',
qualification:'PHD',
emp_dept:'CSE',
city:'xyz'
}
{
emp_id:2,
emp_name:'xyz',
qualification:'PHD',
emp_dept:'CSE',
city:'xyz',
status:1
}
..........

下面是我使用流水线的Go代码

var conditionParam []bson.M
if(city!=""){
conditionParam = []bson.M{
bson.M{"$eq": []string{"$$element.qualification", "PHD"}},
bson.M{"$eq": []string{"$$element.emp_dept", "CSE"}},
bson.M{"$eq": []string{"$$element.city", "xyz"}},
bson.M{"$or": []bson.M{
bson.M{"$$element.status": bson.M{"$exists": false}},
bson.M{"$$element.status": 1}}, 
},
}
}else if(){
--------------------
}
matchStage:=bson.M{"$match":bson.M{'dept_id':1}}
lookupStage:=bson.M{"$lookup": bson.M{
"from":         "employee",
"localField":   "dept_name",
"foreignField": "emp_dept",
"as":           "result_list",
}}
pipeline := getCollection.Pipe([]bson.M{
matchStage,
lookupStage,
{"$addFields": bson.M{
"result_list": bson.M{
"$filter": bson.M{
"input": "$result_list",
"as":    "element",
"cond": bson.M{
"$and": conditionParam,
},
},
},
}},
})

此代码返回错误

Unrecognized expression '$$element.status'

如何使用mgo收集在golang的$and中使用$or

当我在or语句上添加注释时,它会返回结果,但当我使用or时,它给出了错误。有人能建议我如何使用$或内部$和管道吗

var conditionParam []bson.M
if city == "" {
conditionParam = []bson.M{
bson.M{"$eq": []string{"$$element.qualification", "PHD"}},
bson.M{"$eq": []string{"$$element.emp_dept", "CSE"}},
bson.M{"$eq": []string{"$$element.city", "xyz"}},
bson.M{"$or": []interface{}{"$exists", []interface{}{"$$element.status", false}}},
bson.M{"$or": []interface{}{"$$element.status", 1}}, 
}
} 

我试着用这个代码写conditionParam,它对我有效。我还没有检查所有的情况,但我想知道你的建议,写or运算符来检查字段是否存在,如果存在,检查它的值是否正确。

最新更新