This aggregation query performs the following tasks:
- Filters documents that are active and not deleted.
- Groups documents by
media_idand filters groups with more than 5 documents. - Looks up related documents from the
new_media_taskscollection. - Groups the joined documents by
filter_idwithin eachmedia_id. - Returns nested grouped documents with counts, sorted by the number of
filter_idgroups permedia_id.
[
// Step 1: Filter documents where `deleted_at` is null and `status` is "ACTIVE"
{
$match: {
deleted_at: null,
status: "ACTIVE"
}
},
// Step 2: Group documents by `media_id` and count the number of documents per media
{
$group: {
_id: "$media_id",
count: { $sum: 1 }
}
},
// Step 3: Keep only media groups with more than 5 documents
{
$match: {
count: { $gt: 5 }
}
},
// Step 4: Lookup related documents from `new_media_tasks` collection matching by `media_id`
{
$lookup: {
from: "new_media_tasks",
localField: "_id",
foreignField: "media_id",
as: "documents"
}
},
// Step 5: Unwind the `documents` array to work with each document individually
{
$unwind: "$documents"
},
// Step 6: Group documents by both `media_id` and `filter_id`, accumulate documents and count per filter
{
$group: {
_id: {
media_id: "$_id",
filter_id: "$documents.filter_id"
},
count: { $sum: 1 },
documents: { $push: "$documents" }
}
},
// Step 7: Group again by `media_id` to nest filters within each media document
{
$group: {
_id: "$_id.media_id",
filters: {
$push: {
filter_id: "$_id.filter_id",
count: "$count",
documents: "$documents"
}
}
}
},
// Step 8: Add a field with the count of filters for sorting
{
$addFields: {
filters_count: { $size: "$filters" }
}
},
// Step 9: Sort documents descending by the number of filters
{
$sort: { filters_count: -1 }
}
]The output documents will be structured like this:
{
"_id": "media_id_value",
"filters": [
{
"filter_id": "filter_id_1",
"count": number_of_documents_for_this_filter,
"documents": [
{ /* document 1 */ },
{ /* document 2 */ },
...
]
},
...
],
"filters_count": total_number_of_filters
}