Thursday, 22 June 2023

How to lookup with localField and foreignField more than one in MongoDB

  Today I will try to share information on "How to lookup with localField and foreignField more than one in MongoDB", This information may be rarely short because just seeing a piece of code only. But this code work for MongoDB current version.

So let's see a piece of code below  :

  db.detailactivities.aggregate([{$lookup:
    {
      from: "activities",
      localField: "user_id",
      localField: "activity_id",
      foreignField: "user_id",
      foreignField: "_id",
     as: "products"
    }
  }]);

Output/result of my project only:

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


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






No comments:

Post a Comment