'why am I only able to see one side data using aggregate function in mongodb?

Image

enter image description here

As you all can see the in the image the postImage is showing Array(0), I have used the following code.

dbs.collection('posts').aggregate([
        {
            $lookup:{
                from: "images",
                localField: "_id",
                foreignField: "object_id",
                as: "postImage"
            }
        },
    ]).toArray((err, res) => {
        if(err) throw err;
        doc.send(JSON.stringify(res));
        dbs.close();
    })

Image of databse:-

post collection

enter image description here

image collection enter image description here

Here posts collections _id matches to the object_id with the images collection. I want to fetch both the data. what should I do?

Thank You in advance.



Solution 1:[1]

I think so it will work after modifying code to this.

 {
  "$project": {
    "_id": {
        "$toString": "$_id"
       }
    }
 },

before using $lookup

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CoderAatmik