I have a two mongoose schema with the following structure.
const ImageSchema = mongoose.Schema({
image: {
type: Schema.Types.ObjectId,
ref: 'Media',
default: null,
},
caption: {
type: String,
trim: true,
default: ''
},
credit: {
type: Schema.Types.ObjectId,
ref: 'User',
default: null,
}
})
const NewsStorySchema = mongoose.Schema({
...,
images: [ImageSchema]
...,
})
In one of the my api endpoints I have used mongoose aggregation. In it, I have successfully used $lookup stage to populate other foreign fields in NewsStory model. But I am unable to figure out how to populate the each image and credit inside images since it is an objectId inside an object, inside an array. Actual data looks something like this.
{
images: [
{
image: "507f191e810c19729de860ea",
caption: "A Rainny Day",
credit: "707f191e810c19729de860eb"
},
{
image: "607f191e810c19729de860ea",
caption: "Sweet Delight",
credit: "827f191e810c19729de860ns"
}
]
}
How may I populate the image and credit using the $lookup stage.