I have two collections: profiles and contents
The profiles collection looks like this:
{
_id: ObjectId('618ef65e5295ba3132c11111'),
blacklist: [ObjectId('618ef65e5295ba3132c33333'), ObjectId('618ef65e5295ba3132c22222')],
//more fields
}
The contents collection looks like this:
{
_id: ObjectId('618ef65e5295ba3132c00000'),
owner: ObjectId('618ef65e5295ba3132c22222'),
//more fields
}
What I need is to get those contents where owner is not included in the blacklist. I thought about to put the blacklist field into the contents documents. I could get the profile by id separately (in another query) and set it manually in the aggregation where I get the contents, but this requires one extra connection.
So my question is: Is there a way to add my profile into each document of another collection? keep in mind that I have the profile ID.
Here is some psuedo code on how it "should" look like, First fetching the blacklists owners, then using that variable as a parameter in the pipeline.
const profileId = input;
const blackListIds = await db.getCollection('profiles').distinct('blacklist', { _id: profileId });
const aggregationResults = await db.getCollection('contents').aggregate([
{
$match: {
owner: {$nin: blackListIds}
}
}
... continuation of pipeline ...
])