I am stuck and need help to get data from another collection based array element from another collection.
My collections are as --
programs.dbs --
{
"_id": ObjectId("61c8f42ec63e700b415b4bed"),
"name": "Java",
"description": "this is a dummy information",
"instructor": ["61c8f6d7e690fc413a075e15", "61c8f6d7e690fc413a071e15", "61c8f0d7e690fc413a071e15"]
}
instructor.dbs --
{
"_id": ObjectId("61c8f6d7e690fc413a075e15"),
"name": "Instrctor1",
"description": "this is a dummy Instructor",
},
{
"_id": ObjectId("61c8f6d7e690fc413a071e15"),
"name": "Instrctor2",
"description": "this is a dummy Instructor",
},
{
"_id": ObjectId("61c8f0d7e690fc413a071e15"),
"name": "Instrctor3",
"description": "this is a dummy Instructor",
}
My query is to find one instructor information based on collection A instructor Array element. I have tried to use this query but getting empty result -
db.getCollection('programs.dbs').aggregate([{$lookup:{from:'instructor.dbs',localField:'instructor',foreignField:"_id",as:'instructors'}}])
Immediate help will be great.
CodePudding user response:
if you are using mongoose maybe the populate method can work.
CodePudding user response:
Your array of instructors should not reference the IDs as strings but as ObjectId(...)
So you would need to have:
"instructor": [ObjectId("61c8f6d7e690fc413a075e15"), ObjectId("61c8f6d7e690fc413a071e15"), ObjectId("61c8f0d7e690fc413a071e15")]
