[英]How to find the required values in the multiple document in mongo DB
I have multiple documents in the collection then I need to find the particular value inside a single document. Is there any way, we can do this? Thanks for any help.
This is the following document that I need to fetch some specific values from the whole collection. 这是以下文档,我需要从整个集合中获取一些特定值。 Its having four collections and we need to find a single value for a particular document inside the collection.
它有四个集合,我们需要为集合中的特定文档找到一个值。
{
"A":{
"A1":"Value A1"
},
"B":{
"B1":"Value B1"
},
"C":{
"C1":"Value C1"
},
"D":{
"D1":"Value D1"
}}
I want to get Value of D1 in the above collection. Is there any way to find it in as single query.
I'm confused a little bit, but maybe you want projections . 我有些困惑,但也许您想要投影 。 You would do something like this (using the java driver ):
您将执行以下操作(使用java驱动程序 ):
findIterable = collection.find(eq("_id", "some id")).projection(include("D.D1"));
This way you get only one record matching the query for which only id
and D.D1
are returned. 这样,您只会得到一条与查询匹配的记录,仅返回
id
和D.D1
。 You can of course change the query to get more records with only specified fields returned. 当然,您可以更改查询以获取仅返回指定字段的更多记录。
If you mean something else, please clarify. 如果您还有其他意思,请澄清。
EDIT : I can see the answer above, so think about using exists
operator as well, something like: 编辑 :我可以看到上面的答案,所以考虑使用以及
exists
运算符,像这样:
findIterable = collection.find(exists("D1")).projection(include("D1"));
This way you get only records having this field and only this field is returned for them (and their ids). 这样,您将仅获得具有该字段的记录,并且仅为其返回此字段(及其ID)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.