Skip to content

Instantly share code, notes, and snippets.

@iamnasirudeen
Created September 19, 2019 10:33
Show Gist options
  • Save iamnasirudeen/ee06b8643857250ba22f0401e3484046 to your computer and use it in GitHub Desktop.
Save iamnasirudeen/ee06b8643857250ba22f0401e3484046 to your computer and use it in GitHub Desktop.
await Category.aggregate([
{
$lookup: {
from: 'articles',
as: 'article',
let: { indicator_id: '$_id' },
pipeline: [
{
$match: {
$expr: { $eq: ['$category', '$$indicator_id'] }
}
},
{ $sort: { createdAt: -1 } },
{ $limit: 4 }
]
}
},
{
$match: {
"article" : {$ne: []}
}
},
{
$sort: {
createdAt: -1
}
},
{
$limit: 2
}
]);
@limistah
Copy link

limistah commented Sep 19, 2019

let categoryPost = await Category.aggregate([
      {
        $lookup: {
          from: 'articles',
          as: 'article',
          let: { indicator_id: '$_id' },
          pipeline: [
            {
              $match: {
                $expr: { $eq: ['$category', '$$indicator_id'] }
              }
            },
            { $sort: { createdAt: -1 } },
            { $limit: 4 }
          ]
        }
      },
      {
        $unwind: "article"
      }
    ]);

Try the above, some syntax might be off, but that is how to do it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment