문제

I have a collection "Course" with this structure:

{ 
  Lessons: [ 
  {
     Title: 'xxx',
     Contents: [ 
     {
       Title: 'yyy',
     },
     ...
     ]
  },
  ...
  ]
}

How can I retrieve the list of Titles of all Contents using MongoDB's aggregation pipeline?

I managed to $unwind all contents this way:

db.Course.aggregate( { $unwind : '$Lessons' }, { $unwind : '$Lessons.Contents' } )

But I can't manage to filter only the Titles within each Content.

도움이 되었습니까?

해결책

You are very close. If I got your requirement right you just need to add a projection:

db.Course.aggregate( 
  { $unwind : "$Lessons" }, 
  { $unwind : "$Lessons.Contents" }, 
  { $project: { _id: 0, test: "$Lessons.Contents.Title" } } 
)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top