简体   繁体   English

如何使用 Java 从 MongoDB 中的所有文档中获取相同键的值?

[英]How do I get values of same key from all documents in MongoDB using Java?

I am new in working on mongodb.我是 mongodb 的新手。 Here is my Mongodb documents I only want the name values.这是我的 Mongodb 文档,我只想要名称值。

{
    "_id" : ObjectId("5da527f784a58d78fcd0b177"),
    "name" : "Jabed",
    "email" : "jabed@gmail.com",
    "Sex" : "male",
    "age" : "19",
    "address" : "Kochukhet, Dhaka",
    "Contct" : "01797259329"
}

{
    "_id" : ObjectId("5da6f415f2bdee4b90cf41d0"),
    "name" : "Bulbul Gulzer Deb",
    "email" : "gulzer.deb@gmail.com",
    "Sex" : "male",
    "age" : "19",
    "address" : "Narinda, Dhaka",
    "Contct" : "01756223666"
}

{
    "_id" : ObjectId("5dd771907607c3e12d9183d7"),
    "name" : "Aniruddha Dey",
    "email" : "aniruddha.dey@gmail.com",
    "Sex" : "male",
    "age" : "14",
    "address" : "Narinda, Dhaka",
    "Contct" : "01745706020"
}

Expected output will be预计 output 将

Jabed
Bulbul Gulzer Deb
Aniruddha Dey

Use the projection:使用投影:

db.yourdocument.find( {}, { name: 1, _id: 0 } )

More control over projected fields can be found here可以在此处找到对投影字段的更多控制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Mongodb中的聚合和相应的java代码获取具有特定字段最小值的所有文档 - how to get all the documents having minimum values of a particular field using aggregation in Mongodb with corresponding java code Java - 如何从JList获取所有选定的值? - Java - How do I get a all the selected values from a JList? 如何在Java中使用Lightcouch API从CouchDB中获取所有文档 - How to get all documents from couchdb using lightcouch api in java 使用Java遍历来自mongodb的所有文档的性能不佳 - Poor performance for traversing all the documents from mongodb using Java MongoDB 如何按两个值的总和对文档进行排序? - MongoDB how do I sort documents by the sum of two values? 如何从 java 中的嵌套 JSON 获取所有键和值 - How to get all key and values from nested JSON in java 如何使用Java从Mongodb中的子文档数组中仅获取所需字段 - How to get just the desired field from an array of sub documents in Mongodb using Java 如何仅使用Java从与特定过滤器匹配的MongoDB文档中获取 - How to only get from MongoDB documents that match a specific filter using Java 如何使用Java从MongoDB中的父路径获取所有子代 - How to get all children from parent path in mongodb using java 如何使用mongodb Java驱动程序更新较旧的文档? - How can I update older documents using mongodb Java driver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM