Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-1180

2 levels nested $lookup

    • Type: Icon: Task Task
    • Resolution: Gone away
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: MongoDB 3.4

      I want to return a list of posts with their owner object, a list of comments of each post that each comment brings to its owner object.

      I try to do this with a nested $ lookup but realize that this is not possible:

          postsdb.aggregate([
        {
          $lookup:
            {
              from: "users",
              localField: "owner",
              foreignField: "_id",
              as: "user"
            },
            $lookup:
            {
              from: "comments",
              localField: "comments",
              foreignField: "_id",
              as: "comments_list",
      
              $lookup:
              {
                from: "users",
                localField: "owner",
                foreignField: "_id",
                as: "user"
            },
          },
       }
        ]).toArray(function(err, docs) {
      console.log(err)
      if (err) return res.status(500).err(err);
      return res.json({posts: docs})
        });
      

      I want to get an object with such a structure:

       posts: array [
          0: {
              title: '',
              owner: {
              userName: '',
              profileImage: ''
              },
              comments: [
                  0: {
                  title: '',
                      owner: {
                          userName: '',
                          profileImage: ''
                      },
                   }]
               },
          1: {
              title: '',
              owner: {
              userName: '',
              profileImage: ''
              },
              comments: [
                  0: {
                  title: '',
                  owner: {
                  userName: '',
                  profileImage: ''
              },
              }]
          },
      ]
      

            Assignee:
            daniel.aprahamian@mongodb.com Daniel Aprahamian (Inactive)
            Reporter:
            kamish shmuel kaminker
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: