2014-04-16 18 views
8

sto cercando di capire come fare nidificati o/E in cui le query come quello che si potrebbe fare in MySQLMongoDB nidificato O/E Dove?

Prendiamo ad esempio

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

risposta

17

La query in MongoDB assomiglia:

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

Documentazione per $and$or

Alcuni esempi e la documentazione ufficiale per MongoDB trova here.

+0

Ho modificato la risposta in modo che contenga la query esatta per la domanda. –

Problemi correlati