Struggling with crafting a Mongoose Aggregate Query to extract the permissions object of a specific member within a deeply nested structure of business data. MongoDB's documentation on this topic is lacking, making it hard for me to progress.
Sample Collection:
const business = [
{
b_id: ObjectId("a"),
locations: [
{
l_id: ObjectId("b"),
teams: [
{
t_id: ObjectId("c"),
members: [
{
m_id: ObjectId("d"),
permissions: {
p1: {
a: true,
b: false,
c: true,
d: false,
},
p2: {
a: true,
b: false,
c: true,
d: false,
}
}
}
]
}
]
}
]
},
]
Expected outcome:
const permissions = {
p1: {
a: true,
b: false,
c: true,
d: false,
},
p2: {
a: true,
b: false,
c: true,
d: false,
}
}
Path:
router.post("/", authUser, (req, res) => {
const _ids = {
b_id: req.body.b_id,
l_id: req.body.l_id,
t_id: req.body.t_id,
m_id: req.body.m_id,
}
Business.aggregate([
{
$match: {
b_id: ObjectId(_ids.b_id),
},
},
{
$project: {
//stuck on projecting the permissions object for the specified member
},
},
]).then((perms) => {
console.log(perms);
});
});
Any guidance would be greatly valued, thank you!