I want to add join collections using $lookup
in mongodb. I am trying as below
{
$lookup:{
from:"User",
localField:"assignedId",
foreignField:"_id",
as:"dataa"}
}
Now I have two collections
User contains objectid
of users like "_id" : ObjectId("56ab6663d69d2d1100c074db"),
and Tasks where it contains assignedId
as a string
"assignedId":"56ab6663d69d2d1100c074db"
Now, when applying $lookup in both collection its not working because Id's are not matching.
For that I googled it and found a solution that to include
{ $project: { assignedId: {$toObjectId: "$assignedId"} }}
but this solution is not working for me, Its throwing an error:
assert: command failed: { "ok" : 0, "errmsg" : "invalid operator '$toObjectId'", "code" : 15999 } : aggregate failed
Please help me how can I resolve this issue.
Thanks