2014-04-16 37 views
8

我想弄清楚如何嵌套或/與那裏像你一樣會在MySQLMongoDB嵌套或/和在哪裏?

做查詢就拿

SELECT 
    first_name, 
    id 
FROM 
    table 
WHERE 
    (
     province = "nb" OR 
     province = "on" 
    ) AND (
     city = "toronto" AND 
     first_name = "steven" 
    ) 

回答

17

MongoDB中查詢看起來像:

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 
    } 
) 

$and的文檔$or

MongoDB的一些示例和官方文檔找到here

+0

我編輯了答案,以便它包含您的問題的確切查詢。 –