Thursday 22 June 2023

How to using aggregate lookup mongoDB in node js API server

 Today i will try share about information "How to using aggregate lookup mongoDB in node js API server", This is information maybe rerely short because just seeing piece of code only. But this code work for mongodb and nodejs currently version.

So lets see piece of code below  :

exports.showAllDetailActivity = async function(req, res){


  dy.aggregate([{$lookup:
    {
     from: "activities",
     localField: "activity_id",
     foreignField: "_id",
     as: "products"
    }
    }]).then((docs)=>{
      res.json(docs)
      })
      .catch((err)=>{
        console.log(err);
        res.status(401).json({ message: 'Something Wrong' });
    });


}

Output / result  of my project only:

[
    {
        "_id": "6493ef3c6ee4fdb45bb6e4db",
        "user_id": "648aecc8ea9841c8bec570a7",
        "activity_id": "6491a39cbb3be69d2f793ae7",
        "doing_activity": "d",
        "description": "d",
        "created": "2023-06-22T06:50:36.199Z",
        "__v": 0,
        "products": [
            {
                "_id": "6491a39cbb3be69d2f793ae7",
                "user_id": "648aecc8ea9841c8bec570a7",
                "activity": "Blogger",
                "description": "Blogger",
                "created": "2023-06-20T13:03:24.682Z",
                "__v": 0
            }
        ]
    }
]


Ok that is just a little information from, hope can helping you. Another question please leave comments below. Thanks for your visiting my blog.


No comments:

Post a Comment