MongoDB嵌套OR / AND在哪里?

时间:2014-04-16 19:26:48

标签: mongodb

我试图找出如何进行嵌套OR / AND,其中查询就像你在MySQL中做的那样

以示例为例

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

1 个答案:

答案 0 :(得分:24)

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