【问题标题】:MongoDB query that gets all unique users获取所有唯一用户的 MongoDB 查询
【发布时间】: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


    【解决方案1】:
    • $unwind解构Users数组
    • $group by Users 对象
    • $replaceRootUsers 对象替换为根
    db.collection.aggregate([
      { $unwind: "$Users" },
      { $group: { _id: "$Users" } },
      { $replaceRoot: { newRoot: "$_id" } }
    ])
    

    Playground


    第二个选项:$replaceRoot 的替代选项,

    • $project 显示必填字段
    db.collection.aggregate([
      { $unwind: "$Users" },
      { $group: { _id: "$Users" } },
      {
        $project: {
          _id: 0,
          name: "$_id.name",
          age: "$_id.age",
          place_of_birth: "$_id.place_of_birth"
        }
      }
    ])
    

    Playground

    【讨论】:

    • 应用此查询时出现错误“无法识别的管道阶段名称:'$replaceRoot'@turivishal
    • 你的mongodb是什么版本的?它应该大于 3.4
    • MongoDB shell 版本 v4.2.13
    • mongodb 和 mongodb shell 版本都不一样,你能检查一下 mongodb 服务器版本,以及你在哪里执行这个查询吗?
    • 对不起,我的错误,服务器版本是MongoDB服务器版本:3.2.11。我目前正在 shell srv 上执行此查询
    猜你喜欢
    • 2020-03-28
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多