我正在尝试通过每个文档中存在的地址对多个文档进行分组。但是,地址本身就是子文档,并存储在数组中。如果它们在数组中甚至存在一个相同的地址,但不一定在相同的索引中,则我需要将两个文档组合在一起。能做到吗?文档的一般结构如下:
{
"_id" : ObjectId("5ccb0983258f7a1694f30e7d"),
"name" : {
"first" : "John",
"middle" : "J",
"last" : "Doe",
"prefix" : "Mr.",
"professionalSuffixes" : [
"MD"
],
"generationalSuffix" : "Jr"
},
"ssn" : "123-45-6789",
"birthDate" : "1996-06-28",
"gender" : "Unknown",
"maritalStatus" : "Separated",
"postalAddresses" : [
{
"streetAddress" : [
"23 LeeWay RD",
"APT 342"
],
"officeSuite" : "4743",
"apartmentNumber" : "022",
"postOfficeBoxNumber" : "12345",
"postalCode" : "12345",
"city" : "St Louis",
"state" : {
"code" : "MO",
"name" : "Missouri",
"description" : "Test Description"
},
"country" : {
"name" : "United States of America",
"iso2Code" : "US",
"iso3Code" : "USA",
"description" : "Test Description"
}
}
]
}
谢谢。
答案 0 :(得分:1)
根据您对我的评论,您只需要先取消该字段:
{
$unwind: "$postalAddresses"
},
{
$group: {
_id: group_cond,
docs_ids: {$push: "$_id"}
}
}
现在组条件应该是使地址“唯一”的任何条件, 它应该看起来像这样:
{ country: "$postalAddresses.country.name", state: "$postalAddresses.state.code", city: "$postalAddresses.city", street: "$postalAddresses.street."}