mongodb mongodb按子字段查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15632525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:08:01  来源:igfitidea点击:

mongodb query by sub-field

mongodbmongodb-query

提问by Linlin

How to query all {"module" : "B"}?

如何查询所有{"module" : "B"}

The following query doesn't work:

以下查询不起作用:

db.XXX.find({ "_id" : { "module" : "B" } });

Thanks a ton!

万分感谢!

There data looks like:

有数据看起来像:

{
  "_id" : {"module" : "A","date" : ISODate("2013-03-18T07:00:00Z")},
  "value" : {"count" : 1.0}
}

{
  "_id" : {"module" : "B","date" : ISODate("2013-03-18T08:00:00Z")},
  "value" : {"count" : 2.0}
}

回答by AdaTheDev

Try:

尝试:

db.XXX.find({ "_id.module" :  "B" });

The difference is your original query would be trying to match on that entire subdocument (i.e. where _id is a subdocument containing a "module" field with value "B" and nothing else)

不同之处在于您的原始查询将尝试匹配整个子文档(即其中 _id 是包含值为“B”的“模块”字段的子文档,没有其他内容)

Reference: MongoDB Dot Notation

参考:MongoDB 点符号

回答by shx2

Use dot notation:

使用点符号:

db.XXX.find({ "_id.module" : "B" })

回答by NPKR

For Exact match on Subdocument

对于子文档的完全匹配

    db.bios.find(
   {
     '_id.module': 'B'
   }
)

the query uses dotnotation to access fields in a subdocument:

该查询使用dot符号来访问 a 中的字段subdocument

Refference link

参考链接