Created
January 26, 2024 11:31
-
-
Save eimg/07e5d151753115202b8be7b1afb930e0 to your computer and use it in GitHub Desktop.
Mongo Aggregate Pipeline Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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