MongoDB Nested OR/AND Where?

前端 未结 1 675
面向向阳花
面向向阳花 2020-12-10 12:11

I am trying to figure out how to do nested OR/AND where queries like what you would do in MySQL

Take for example

SELECT
    first_name,
    id
FROM
          


        
1条回答
  •  时光说笑
    2020-12-10 12:58

    The query in MongoDB looks like:

    Database.collection_name.find(
        // This is the condition
        {
            $and: [
               {
                   $or: [
                       {province: 'nb'},
                       {province: 'on'}
                   ]
               },
               {
                   city: "toronto"
               },
               {
                   first_name: "steven"
               }
            ]
        },
    
        // Specify the fields that you need
        {
            first_name: 1,
            _id: 1
        }
    )
    

    Documentation for $and $or

    Some examples and the official documentation for MongoDB find here.

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