有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我是mongodb的新手。这是我的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"
}

预期产出将为

Jabed
Bulbul Gulzer Deb
Aniruddha Dey

共 (1) 个答案

  1. # 1 楼答案

    使用投影:

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

    可以在here中找到对投影字段的更多控制