【问题标题】:MongoDB Nested OR/AND Where?MongoDB 嵌套 OR/AND 在哪里?
【发布时间】:2014-04-16 19:26:47
【问题描述】:

我正在试图弄清楚如何进行嵌套 OR/AND 查询,就像您在 MySQL 中所做的那样

举个例子

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

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    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

    【讨论】:

    • 我已经编辑了答案,使其包含您问题的确切查询。
    猜你喜欢
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 2013-09-08
    • 2014-09-27
    相关资源
    最近更新 更多