wend try with this query, return the lookup is empty
db.getCollection(\'tests\').aggregate([
{$match: {typet:\'Req\'}},
{$project: {incharge:1}},
Comparing a string
with an ObjectId
doesn't throw an error, rather sends an empty array in the aggregated output document. So you need to make sure that you have converted the string
object id to mongodb's ObjectId
:
db.getCollection('tests').aggregate([
{$match: {typet:'Req'}},
{$set: {incharge: {$toObjectId: "$incharge"} }}, // keep the whole document structure, but replace `incharge` into ObjectId
{$lookup:{
from: "users",
localField: "incharge", //this is the _id user from tests
foreignField: "_id", //this is the _id from users
as: "user"
}}
])