MongoDB嵌套OR/AND在哪里? [英] MongoDB Nested OR/AND Where?

查看:73
本文介绍了MongoDB嵌套OR/AND在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出如何执行嵌套OR/AND的查询,例如在MySQL中将要执行的查询

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

例如

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

推荐答案

MongoDB中的查询如下:

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

$ and 的文档 MongoDB的一些示例和官方文档可在此处.

Some examples and the official documentation for MongoDB find here.

这篇关于MongoDB嵌套OR/AND在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆