2017-09-07 49 views
0

我在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" 
       } 
      ] 
     } 
    ] 
} 
+1

讓我們知道您曾經試圖 – sidgate

+0

這裏是我用了 db.getCollection( '出貨')骨料(查詢。{$項目:{ 'Carriers.Tracking.TrackingNo':1}},{$開卷: '$運營商'})相當()。 – user1903796

+0

[MongoDB:如何在一個文檔中對嵌套數組進行分組?](https://stackoverflow.com/questions/46450119/mongodb-how-to-group-nested-arrays-in-one-document) – sidgate

回答

0

如你有陣列在陣列中,您首先需要放鬆兩次然後再次分組。以下查詢應該工作。您需要根據需要在$ group和$ project中投影額外的字段。

db.shipments.aggregate(
    {$unwind:'$Carriers'}, 
    {$unwind: '$Carriers.Tracking'}, 
    {$group: {_id:'$_id', c: {$push: '$Carriers.Tracking'}}}, 
    {$project:{Carriers: {Tracking: '$c'}}} 
) 
0

根據上述問題中提到的描述,請嘗試執行以下聚合查詢到MongoDB shell作爲上述問題的解決方案。

db.shipments.aggregate(

// Pipeline 
[ 
    // Stage 1 
    { 
     $unwind: { 
      path: '$Carriers' 
     } 
    }, 

    // Stage 2 
    { 
     $unwind: { 
      path: '$Carriers.Tracking' 
     } 
    }, 

    // Stage 3 
    { 
     $group: { 
      _id: { 
       _id: '$_id', 
       Sid: '$Sid' 
      }, 
      Carriers: { 
       $addToSet: { 
        TrackingNo: '$Carriers.Tracking.TrackingNo' 
       } 
      } 

     } 
    }, 

    // Stage 4 
    { 
     $group: { 
      _id: '$_id', 
      Carriers: { 
       $addToSet: { 
        Tracking: '$Carriers' 
       } 
      } 
     } 
    }, 

    // Stage 5 
    { 
     $project: { 
      _id: '$_id._id', 
      Sid: '$_id.Sid', 
      Carriers: '$Carriers' 
     } 
    } 

] 

);

相關問題