Reputation: 6887
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.
Upvotes: 0
Views: 1255
Reputation: 17433
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" } }
)
Upvotes: 1