Mongo DB按字段名称查找任何值 [英] Mongo DB find by field name with any value

查看:60
本文介绍了Mongo DB按字段名称查找任何值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找所有具有此字段的文档,它包含的确切值无关紧要;我只对它的存在感兴趣.

I am trying to find all documents that have this field, it doesn't matter what exact value it contains; I am only interested in the existence of it.

这里是一个例子:

{  
   "payload":{  
      "products":{  
         ...
      },
      "discount":{  
         "type":"1%",
         "value":"1",
         "name":"New Year Discount 1%"
      }
   }
}

我需要定位任何具有名称为discount的字段的文档.如何编写查询以获取此类文档?

I need to target any document that has a field with the name discount. How can I write a query to get such documents?

推荐答案

有一个 $ exists 运算符:

db.collectionName.find({"payload.discount": {$exists: true}})

这篇关于Mongo DB按字段名称查找任何值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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