将两个$ exists合并到MongoDB .find [英] Combining two $exists en MongoDB .find

查看:56
本文介绍了将两个$ exists合并到MongoDB .find的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用 $ exists 查找包含某些字段的文档.我尤其对两个字段感兴趣: payload.fields.MDI_CC_DIAG_DTC_LIST payload.asset .

I would like to find documents that contain some fields, by using $exists. In particular, I am interested in two fields: payload.fields.MDI_CC_DIAG_DTC_LIST and payload.asset.

如果我只想查找包含第一个文档的文档,这是我的查询:

If I want to find only documents that contain the first one, this is my query:

db.getCollection("dtc").find({"payload.fields.MDI_CC_DIAG_DTC_LIST": {$exists: true}}).count()

它给了我2265个文档.

It gives me 2265 documents.

当我尝试查找包含两个字段(上述字段)的文档时,我使用以下代码:

When I try to find documents that contain two fields (the ones described above), I use this code:

db.getCollection("dtc").find({"payload.fields.MDI_CC_DIAG_DTC_LIST": {$exists: true}}, {"payload.asset": {$exists: true}}, {multi: true})count()

它给我抛出一个错误:

无法检索文档

[coches.dtc@ localhost:27017 [direct]] : An error occurred while retrieving documents!

Stacktrace: 
|_/ java.lang.Exception: [coches.dtc@ localhost:27017 [direct]] : An error occurred while retrieving documents!
|____/ Illegal argument: Invalid number of arguments to find().

我写错了什么?

推荐答案

您的查询有两个问题,请尝试以下一个问题:

Your query has couple of issues, try below one :

db.getCollection("dtc")
  .find({
    "payload.fields.MDI_CC_DIAG_DTC_LIST": { $exists: true },
    "payload.asset": { $exists: true }
  })
  .count();

问题:

  1. .find()会接受两个参数 .find({...},{...})第一个作为过滤器(所有针对集合的过滤器都在此处)&第二个是投影(用于从结果文档中排除或包括某些字段).在这里,您要传递3个参数.但通常来说,涉及到 node.js 的第三个函数可能是回调函数,但它与在数据库上执行的实际查询无关.
  2. .find()上没有称为 {multi:true} 的东西. multi 将作为第三个选项/arg传递给
  1. .find() would take two arguments .find({...},{...}) first one being filter (All filters against collection go here) & second one is projection (Which is used to either exclude or include certain fields from result documents). Here you're passing in 3 args. But in general when it comes to node.js 3rd one could be a callback function but it has nothing to do with actual query being executed on database.
  2. There is no such thing called {multi: true} on .find(). multi will be passed as 3rd option/arg to .update() operations in order to update multiple documents matching filtered criteria.

这篇关于将两个$ exists合并到MongoDB .find的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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