如何按 mongoDB 中的嵌套文档分组

how to group by nested documents in mongoDB

我最近开始学习 mongoDB 并做一些例子来提高自己。我有一个问题,我无法想象我怎么能在 mongoDB 中做到这一点,或者这真的有可能得到。所以我需要你的帮助。

首先,我有一个这样的collection;

/* 1 */
{
    "_id" : ObjectId("62372c37ea4cbb005e97ec1c"),
    "CreatedTime" : ISODate("2022-03-20T13:29:27.456Z"),
    "UpdatedTime" : Date(-62135596800000),
    "Name" : "Name1",
    "Surname" : "Surname1",
    "Company" : "Company1",
    "ContactInformation" : null
}

/* 2 */
{
    "_id" : ObjectId("62372c37ea4cbb005e97ec1d"),
    "CreatedTime" : ISODate("2022-03-20T13:29:27.456Z"),
    "UpdatedTime" : Date(-62135596800000),
    "Name" : "Name2",
    "Surname" : "Surname2",
    "Company" : "Company2",
    "ContactInformation" : [ 
        {
            "InfoType" : 1,
            "Info" : "+905554443322"
        }, 
        {
            "InfoType" : 3,
            "Info" : "İstanbul"
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("62372c37ea4cbb005e97ec1e"),
    "CreatedTime" : ISODate("2022-03-20T13:29:27.456Z"),
    "UpdatedTime" : Date(-62135596800000),
    "Name" : "Name3",
    "Surname" : "Surname3",
    "Company" : "Company3",
    "ContactInformation" : [ 
        {
            "InfoType" : 1,
            "Info" : "+905554443301"
        }, 
        {
            "InfoType" : 1,
            "Info" : "+905554443302"
        }, 
        {
            "InfoType" : 3,
            "Info" : "Kastamonu"
        }
    ]
}

/* 4 */
{
    "_id" : ObjectId("62372c37ea4cbb005e97ec1f"),
    "CreatedTime" : ISODate("2022-03-20T13:29:27.456Z"),
    "UpdatedTime" : Date(-62135596800000),
    "Name" : "Name4",
    "Surname" : "Surname4",
    "Company" : "Company4",
    "ContactInformation" : [ 
        {
            "InfoType" : 3,
            "Info" : "Kastamonu"
        }
    ]
}

信息类型:1 - Phone 数字,3 - 位置(城市、国家等) Info 是 InfoTypes

的值

然后我想得到一个包含三个值的报告;

预期输出:

{
 location: "İstanbul",
 recordCount: 1,
 phoneNumCount: 1
},
{
 location: "Kastamonu",
 recordCount: 2,
 phoneNumCount: 2
}

前两个条件可以,我可以,但是第三个我不能。

谢谢大家的帮助

尝试以下管道:

  1. 查找用户所在的国家/地区并使用 $addFields 将其添加为一个字段。如果 null.
  2. 也将 ContactInformation 添加为空数组
  3. 使用$group to group documents by user's country. Then use $count to count number of records and $sum拨打那个国家的电话号码。
[
  {
    $addFields: {
      country: {
        $arrayElemAt: [
          {
            $filter: {
              input: "$ContactInformation",
              as: "info",
              cond: {
                $eq: [
                  "$$info.InfoType",
                  3
                ]
              }
            }
          },
          0
        ]
      },
      ContactInformation: {
        $ifNull: [
          "$ContactInformation",
          []
        ]
      }
    }
  },
  {
    $group: {
      _id: "$country.Info",
      recordCount: {
        $count: {}
      },
      phoneNumCount: {
        $sum: {
          $size: {
            $filter: {
              input: "$ContactInformation",
              as: "info",
              cond: {
                $eq: [
                  "$$info.InfoType",
                  1
                ]
              }
            }
          }
        }
      }
    }
  }
])

Mongo Playground