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

查看:16
本文介绍了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天全站免登陆