查找字段不存在的所有文档,加上如果字段存在则应用条件 [英] Find all docs where field doesn't exists, plus if field exists apply condition

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

问题描述

有没有办法进行如下查询:

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天全站免登陆