查找不存在该字段的所有文档,以及存在该字段的适用条件 [英] Find all docs where field doesn't exists, plus if field exists apply condition

查看:46
本文介绍了查找不存在该字段的所有文档,以及存在该字段的适用条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以进行以下查询:

Is there a way to make a query that:

  • 如果某些字段存在,它将在该字段上应用条件,如果通过,它将添加返回文档并将其添加到结果中.
  • (如果该字段不存在,是否还会将文档添加到结果中?)
  • if certain field exists, it will apply condition on the field and if it pass, it will add return document and add it to the results.
  • and if such field doesn't exist, it will also add document to the results?

最后,我们将收到某些字段符合条件的所有文档 AND 根本不存在该字段的所有文档.

In the end we will receive all documents where certain field have matched condition AND all document where such field doesn't exist at all.

推荐答案

这样的事情如何?

db.stackoverflow.find({
  $or: [
    { howmuch: { $exists:false } },
    { howmuch:5 }
  ]})

在stackoverflow集合中,这将查找所有没有howmuch字段的文档以及所有已经将howmuch设置为5的文档.

In the stackoverflow collection, this will find all documents that do not have the howmuch field plus all documents that do have howmuch set to 5.

这篇关于查找不存在该字段的所有文档,以及存在该字段的适用条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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