简体   繁体   English

如何在Mongo DB中的多个文档中查找所需的值

[英]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. 这样,您只会得到一条与查询匹配的记录,仅返回idD.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.

相关问题 Mongo DB将值列表作为单独的文档插入 - Mongo DB insert list of values as separate document 如何更新具有复杂结构的Mongo DB文档 - How to update Mongo DB document with complex structure 如何使用Java Mongo DB驱动程序版本3将BasicDBObject转换为Mongo文档? - How to convert a BasicDBObject to a Mongo Document with the Java Mongo DB driver version 3? 如何使用JAVA一次更新mongo db的单个文档中的多个字段(数组字段和普通字段)? - How to update multiple fields(array field and normal field) in a single document of mongo db at a time using JAVA? 如何使用JAVA在MONGO DB中存储值? - How to store values in MONGO DB using JAVA? Mongo DB将不会更新现有文档 - Mongo db will not update an existing Document 如何使用Morphia在Mongo DB文档中存储作为类的子类的属性? - How to store a property that is subclass of class in Mongo DB document using Morphia? 如何使用 Spring Data Mongo DB 仅检索文档的特定字段? - How to retrieve only specific field of a document with Spring Data Mongo DB? 将多个用户输入值插入 Mongo db 集合时出现异常 - Exception while inserting multiple user input values to Mongo db collection 如何在内部地图中通过参数找到mongo文档(更好地使用Spring MongoTemplate) - How to find mongo document by parrameter in internal map (better with Spring MongoTemplate)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM