I want to pass the report id variable into the pipeline stage of the user’s collection to filter the (clear) field array to get only objects owned by this report
const reports = await Report.aggregate([
{
$lookup: {
from: 'operations',
let: { operationId: '$operation', reportId: '$_id' /* I don't know if this is right or if there's a better way to get the report id */ },
pipeline: [
{
$match: { $expr: { $eq: ['$_id', '$$operationId'] } },
},
{
$lookup: {
from: 'users',
let: {
userId: '$initiator.user', type: '$initiator.type'},
pipeline: [
{ $match: { $expr: { $eq: ['$_id', '$$userId'] } } },
{
$project: {
color: '$colorCode.code',
avatar: 1,
type: '$$type',
clear: {
$filter: {
input: '$clear',
as: 'clear',
cond: {
$eq: [
'$clear.report',
'6375856e1f7e265016ffd3c8', /* here i want to pass the reporId */
],
},
},
},
},
},
],
as: 'initiator',
},
},
{
$lookup: {
from: 'users',
let: {
userId: '$peer.user', type: '$peer.type',
},
pipeline: [
{ $match: { $expr: { $eq: ['$_id', '$$userId'] } } },
{
$project: {
color: '$colorCode.code',
avatar: 1,
type: '$$type',
clear: {
$filter: {
input: '$clear',
as: 'clear',
cond: {
$eq: [
'$clear.report',
'6375856e1f7e265016ffd3c8', /* same here */
],
},
},
},
},
},
],
as: 'peer',
},
},
{
$unwind: '$initiator',
},
{
$unwind: '$peer',
},
],
as: 'operation',
},
},
{
$unwind: '$operation',
},
])