MongoDB: How to group nested arrays in one document?

后端 未结 1 468
深忆病人
深忆病人 2021-01-27 12:59

I have the following Collection:

{
    id: 23423-dsfsdf-32423,
    name: Proj1,
    services: [
         {
            id:sdfs-24423-sdf,
            name:P1_Ser         


        
相关标签:
1条回答
  • 2021-01-27 13:53

    You need to group on null _id so that all services get grouped in single document. Also $unwind the services array before grouping, else group will give you array of arrays

    db.project.aggregate(
      {$unwind: '$services'},
      {$group: {_id:null, services: {$push: '$services'}}}
    )
    
    0 讨论(0)
提交回复
热议问题