mongodb query using _id object in lookup localField

Viewed 6130

we are trying to match _id object to mId foreignfield which is not working. looking for mondodb docs. they do not have anything on this. Is it possible with mongodb query or not?

_id as object in document

   "_id" : ObjectId("56ab6663d69d2d1100c074db"),

mId as String in document

"mId" : "56ab6663d69d2d1100c074db",

query as below:

 collection.aggregate([
                {
                  $lookup:
                    {
                      from: "category",
                      localField: "_id",
                      foreignField: "mId",
                      as: "categories"
                    }
               }
            ])

UDPATE

as a summary, mongodb does not support type coercion in $lookup. so to need above working i have to have _id and mId as ObjectId type in documents itself to make it work.

2 Answers

You can join id with mid.Though $lookup does not support coercion,use $project pipeline stage and first convert all the mid to Object Id and then try to $lookup

Related