how can i join collections based on a field value?
this is my collection named “content” which actually just has references to “audios” and “videos” collections
[
{
createdAt: "2022-01-24T18:46:28.781+00:00",
refId: ObjectId("6262d0d9302ff436aa2028f3"),
collection: "audios",
...
},
{
createdAt: "2022-01-25T18:46:28.781+00:00",
refId: ObjectId("625007153e8aaaa2b275f475"),
collection: "videos",
...
},
]
what i want is just joining collections using the “collection” field value in $lookup’s from field like this
db.content.aggregate([
{
$lookup: {
from: '$collection',
let: { col: '$refId' },
pipeline: [
{ $match: { $expr: { $eq: ['$_id', '$$col'] } } },
{ $project: { _id: 0, title: 1, description: 1 } },
],
as: 'col',
},
},
{ $unwind: { path: '$col', preserveNullAndEmptyArrays: true } },
])
what i was expecting as a results is:
[
{
createdAt: "2022-01-24T18:46:28.781+00:00",
col: {
title: "NEW AUDIO",
description: "description...."
},
collection: "audios",
...
},
{
createdAt: "2022-01-25T18:46:28.781+00:00",
col: {
title: "NEW VIDEO",
description: "description...."
},
collection: 'videos',
...
},
]
but it does not work in this way and not joining collections based on the “collection” field value