Skip to content

Instantly share code, notes, and snippets.

@eimg
Created January 26, 2024 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eimg/07e5d151753115202b8be7b1afb930e0 to your computer and use it in GitHub Desktop.
Save eimg/07e5d151753115202b8be7b1afb930e0 to your computer and use it in GitHub Desktop.
Mongo Aggregate Pipeline Example
const pipeline = [
{
$match: { type: 'post' },
},
{
$match: {
owner: { $in: ['1', '2', '3'] }
},
},
{
$match: {
name: new RegExp(`.*${q}.*`, "i"),
},
},
{
$sort: { _id: -1 },
},
{
$limit: 40,
},
{
$lookup: {
from: "users",
localField: "_id",
foreignField: "user_id",
as: "user",
pipeline: [
{
$match: { role: "manager" },
},
{
$lookup: {
from: "comments",
localField: "_id",
foreignField: "user_id",
as: "feedbacks",
pipeline: [
{
$match: { type: "feedback" },
},
],
},
},
],
},
},
];
module.exports = { pipeline };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment