亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

$or 內 $and 在 $filter 條件下不起作用

$or 內 $and 在 $filter 條件下不起作用

Go
滄海一幻覺 2022-06-27 15:08:02
我正在將 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中使用$orinside ?$and當我對or語句發表評論時,它返回結果,但是當我使用它時,or它給出了錯誤。任何人都可以建議我如何在 $ 和管道中使用 $ 或
查看完整描述

1 回答

?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

 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運算符來檢查字段是否存在以及是否存在檢查其值是否正確。


查看完整回答
反對 回復 2022-06-27
  • 1 回答
  • 0 關注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號