mongodb queries both with AND and OR

后端 未结 4 1918
情歌与酒
情歌与酒 2020-12-13 00:10

can I use combination of OR and AND in mongodb queries?

the code below doesn\'t work as expected

db.things.find({
           $and:[
                {         


        
4条回答
  •  有刺的猬
    2020-12-13 00:40

     db.things.find( {
          $and : [
                   { 
                     $or : [ 
                             {"first_name" : "john"},
                             {"last_name" : "john"}
                           ]
                   },
                   { 
                     "Phone":"12345678"
                   }
                 ]
        } )
    

    AND takes an array of 2 expressions OR , phone.
    OR takes an array of 2 expressions first_name , last_name.

    AND

    • OR

      • first_name
      • last_name
    • Phone Number.

    Note: Upgrade to latest version of MongoDB, if this doesn't work.

提交回复
热议问题