【发布时间】:2021-04-24 15:06:27
【问题描述】:
我目前正在使用 MongoDB 数据库。我的问题是我有很多同名的人,但他们是不同的人,有不同的年龄、出生地等。
文档示例如下:
{
"_id" : ObjectId("6072fee3145c156c123ce3"),
"Users" : [
{
"name" : "John Davies",
"age" : NumberLong(35),
"place_of_birth" : "Cardigan"
},
{
"name" : "Edward Jones",
"age" : "blank",
"place_of_birth" : "Liverpool"
},
{
"name" : "Daniel Rhys",
"age" : NumberLong(63),
"place_of_birth" : "Cardigan"
},
{
"name" : " Evan Williams",
"age" : NumberLong(61),
"place_of_birth" : "Cardigan"
},
{
"name" : "John Davies ",
"age" : NumberLong(21),
"place_of_birth" : "Cardigan"
}
]
}
{
"_id" : ObjectId("6072fee3145c156c321ef6"),
" Users " : [
{
"name" : "John Davies",
"age" : NumberLong(35),
"place_of_birth" : "Swansea"
},
{
"name" : "Edward Jones",
"age" : "blank",
"place_of_birth" : "Liverpool"
},
{
"name" : "Daniel Rhys ",
"age" : NumberLong(63),
"place_of_birth" : "Barry"
},
{
"name" : "Evan Williams",
"age" : NumberLong(61),
"place_of_birth" : "Cardigan"
},
{
"name" : "John Davies",
"age" : NumberLong(21),
"place_of_birth" : "Cardigan"
}
]
}
所以我的目标是查询这些数据,以便获得所有姓名以及每个唯一人员的年龄和出生地一次。
所以输出应该包含(我只是放入一个表格以更清楚地显示查询结果应该是什么):
| Name | Age | Birth place |
|---|---|---|
| John Davies | 35 | Cardigan |
| Edward Jones | Blank | Liverpool |
| Daniel Rhys | 63 | Cardigan |
| Evan Williams | 61 | Cardigan |
| John Davies | 21 | Cardigan |
| Daniel Rhys | 63 | Barry |
| John Davies | 35 | Swansea |
非常感谢任何有关最佳方法的建议或指导
【问题讨论】:
标签: mongodb aggregation-framework