我在MongoDB中擁有這些數據,我想通過此數據執行group by以獲取所有跟蹤編號,並以同一個id在單個數組格式下。如何在mongodb中執行group
* 1 */
{
"_id" : ObjectId("597056182a93692b4c7691bf"),
"Sid":1,
"Carriers" : [
{
"Tracking" : [
{
"TrackingNo" : "121_dom"
}
]
},
{
"Tracking" : [
{
"TrackingNo" : "779591314278"
},
{
"TrackingNo" : "779591314039"
},
{
"TrackingNo" : "779591314231"
}
]
}
]
}
/* 2 */
{
"_id" : ObjectId("597057338c65c002e4285fb3"),
"Sid":2,
"Carriers" : [
{
"Tracking" : [
{
"TrackingNo" : "122_dom"
}
]
},
{
"Tracking" : [
{
"TrackingNo" : "77959131427"
},
{
"TrackingNo" : "77959131403"
}
]
}
]
}
/* 3 */
{
"_id" : ObjectId("5980ae7ecc71b581b626d20b"),
"Sid":3,
"Carriers" : [
{
"Tracking" : [
{
"TrackingNo" : "123_dom"
}
]
},
{
"Tracking" : [
{
"TrackingNo" : "77959131408"
},
{
"TrackingNo" : "779591314059"
},
{
"TrackingNo" : "779591315551"
}
]
}
]
}
即在由上_id,其示出了在一個單一的陣列在相同的_id所有跟蹤號的操作進行組,意味着結果集將是這樣的:
{
"_id" : ObjectId("597056182a93692b4c7691bf"),
"Sid":1,
"Carriers" : [
{
"Tracking" : [
{
"TrackingNo" : "121_dom"
}
{
"TrackingNo" : "779591314278"
},
{
"TrackingNo" : "779591314039"
},
{
"TrackingNo" : "779591314231"
}
]
}
]
}
讓我們知道您曾經試圖 – sidgate
這裏是我用了 db.getCollection( '出貨')骨料(查詢。{$項目:{ 'Carriers.Tracking.TrackingNo':1}},{$開卷: '$運營商'})相當()。 – user1903796
[MongoDB:如何在一個文檔中對嵌套數組進行分組?](https://stackoverflow.com/questions/46450119/mongodb-how-to-group-nested-arrays-in-one-document) – sidgate