MoninDB中的$ in和$ or可以互相替换吗? [英] Can $in and $or replace each other in MongoDB?

查看:64
本文介绍了MoninDB中的$ in和$ or可以互相替换吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ in和$ or在MongoDB中可以互相替换吗?

Can $in and $or replace each other in MongoDB?

db.restaurants.find(
{
  "borough" :{$in :["Staten Island","Queens","Bronx","Brooklyn"]}},
  {
    "restaurant_id" : 1,
    "name":1,"borough":1,
    "cuisine" :1
  }
);


db.restaurants.find(
{ 
"borough": "Bronx" , 
  $or : [
          { "cuisine" : "American " },
          { "cuisine" : "Chinese" }
        ] 
} 
);

在这里,我发现这两个查询都要求我们从一些选项中进行选择:

Here I observe that both these queries require us to choose from some options:

在第一个查询中用$ or替换$ in如下是否有意义:

Does it make sense to replace $in in the first query with $or as follows:

db.restaurants.find(
{ $or: [{ borough: 'Staten Island',
          borough: 'Queens',
          borough: 'Bronx',
          borough: 'Brooklyn' }],

  { _id : 1,
    name: 1,
    borough : 1,
    cuisine : 1
  }
 })

$ in和$ or可以替换吗?

Are $in and $or replaceable?

更新:
我尝试使用两个查询以期获得相同的结果:

Update:
I tried to use two queries in a hope to get identical results:

第二个查询为什么只选择状态为"D"的两行?

Why is the second query selecting two rows of status 'D' only?

> db.inventory.find( {status : { $in: [ 'A', 'D'] }}, {item:1, status: 1})
{ "_id" : ObjectId("5eb67598bee5213484d45087"), "item" : "journal", "status" : "A" }
{ "_id" : ObjectId("5eb67598bee5213484d45088"), "item" : "notebook", "status" : "A" }
{ "_id" : ObjectId("5eb67598bee5213484d45089"), "item" : "paper", "status" : "D" }
{ "_id" : ObjectId("5eb67598bee5213484d4508a"), "item" : "planner", "status" : "D" }
{ "_id" : ObjectId("5eb67598bee5213484d4508b"), "item" : "postcard", "status" : "A" }
>
>
> db.inventory.find( {$or: [ {status: 'A', status: 'D'} ] }, {item:1, status: 1})
{ "_id" : ObjectId("5eb67598bee5213484d45089"), "item" : "paper", "status" : "D" }
{ "_id" : ObjectId("5eb67598bee5213484d4508a"), "item" : "planner", "status" : "D" }
> 

推荐答案

摘自其官方文档本身

在使用$或时,将对$进行相等性检查同一字段的值,请使用$ in运算符代替$ or运算符.

When using $or with that are equality checks for the value of the same field, use the $in operator instead of the $or operator.

如果您有以下文档:

[
  {
    "price": 100
  },
  {
    "price": 200
  },
  {
    "price": 300
  },
  {
    "price": 400
  },
  {
    "price": 500
  }
]

如果您要获取 price 等于 100 500 的文档,请进行如下查询:

If you wanted to get docs where price is equal to 100 or 500, query like :

db.collection.find({ price: { $in: [ 100, 500 ] } })

通过如上所述的操作,查询很简单&干净的.您也可以使用 $ or 而不是 $ in ,但是为什么您会放弃速记符号并通过一次又一次地添加更多相同字段的对象来使查询看起来笨重呢?

By doing like above, query is simple & clean. You can also use $or instead of $in but why would you loose shorthand notation and try to make your query look bulky by adding more objects of same field again and again ?

默认情况下,如果要在两个不同的运算符上执行逻辑 OR ,则可以使用 $ or ,但是何时在 $ or 上使用同一字段:

By default if you wanted to do logical OR on two different operators you would use $or, But when to use $or on same field :

db.collection.find({ $or: [ { price: { $lt: 200 } }, { price: { $gt: 400 } } ] })

就像上面一样,当您有多个不同的条件要在同一字段上进行匹配时,就会使用它.

As like above when you've multiple different conditions to match on same field you'll use it.

这两个查询在执行但使用 $ in 时会产生相同的结果-如果输入值为正数或可以是字符串或其他类型,其中输入值将与的值完全匹配文档中的价格字段,但是当您使用 $ or 时,您正在检查同一字段上的不同条件.

These two queries yield same result when executed but when you use $in - if input values are straight numbers or can be strings or other types where input values will exactly match with values of price field in docs, but when you use $or you're checking for different conditions on same field.

测试: mongoplayground

这篇关于MoninDB中的$ in和$ or可以互相替换吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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