Using $exists in a MongoDB expression

前端 未结 1 1907
离开以前
离开以前 2021-01-21 07:22

For background, if I want to compare two fields, I can\'t use the following syntax (because it compares to the literal string \"$lastName\" rather than the contents of the $last

1条回答
  •  鱼传尺愫
    2021-01-21 07:45

    You will need to use the $or logical operator to do this.

    {
       "$or": [
          {
             "$expr": {
                "$ne": [
                   "$firstName",
                   "$lastName"
                ]
             }
          },
          {
             "fullName": {
                "$exists": true
             }
          }
       ]
    }
    

    You last query failed because mongod thinks $exists is the expression you are passing the the $expr operator.

    0 讨论(0)
提交回复
热议问题