mongodb, mgo 컬렉션과 함께 golang을 사용하고 있습니다.
내 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에서 어떻게 $or
내부 $and
를 사용할 수 있습니까?
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
필드가 있는지 확인 하는 연산자를 작성하는 것이 올바른 방법 이며 존재하는 경우 값을 확인하십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다