我最近开始学习mongoDB,并做了一些例子来提高自己。我有一个问题,我无法想象如何在 mongoDB 中做到这一点,或者这真的有可能实现。所以我需要你的帮助。
首先,我有一个这样的收藏;
/* 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 - 电话号码,3 - 位置(城市、国家等) Info 是 InfoTypes 的值
然后我想得到一个包含三个值的报告;
- 位置信息(如果文档有 InfoType 为 3 的嵌套文档)
- 所属位置的记录数
- 属于该位置的电话号码
预期输出:
{
location: "İstanbul",
recordCount: 1,
phoneNumCount: 1
},
{
location: "Kastamonu",
recordCount: 2,
phoneNumCount: 2
}
前两个条件可以,我可以得到它们,但第三个条件我不能。
感谢大家的帮助
最佳答案
尝试以下管道:
- 查找用户所在的国家/地区并使用
$addFields
将其添加为字段。如果null
,还要添加ContactInformation
作为空数组。 - 使用
$group
按用户所在国家/地区对文档进行分组。然后使用$count
计算记录数和$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
]
}
}
}
}
}
}
}
])
关于mongodb - 如何在 mongoDB 中按嵌套文档进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71547894/