0

I am building an apllication with mongoose and node.js that enables you to post, comment, and like both the posts and the comments.

I am tryign to build a query that gets all the information from the db.

saw that answer nested 3 level lookup like in here MongoDB nested lookup with 3 levels. it works, but the project filter returns me an error and if I remove it, I get some comments that looks like that:

"comments": 
      {
        "user": [],
        "likes": []
      }
    

the argigate

 postArgigate =  [
    {
        //lookup for the post likes
        $lookup: {
            from: 'likes',
            localField: '_id',
            foreignField: 'postCommentID',
            as: '_likes'
        },
    },
    //the user wrote the post
    {
       $lookup: {
           from: 'users',
           localField: 'userID',
           foreignField: '_id',
           as: 'user'
       },
   },
   
     {
        $lookup: {
            from: 'comments',
            localField  : 'commentsID',
            foreignField  : '_id',
            as: 'comments'
        },
     },
     //unwind the comments to do a nesting lookup
     {
       $unwind: {
         path: "$comments",
         preserveNullAndEmptyArrays: true
    }
   }, 
   //lookup in the comments likes 
    {
       $lookup: {
           from: 'likes',
           localField  : 'comments._id',
           foreignField  : 'postCommentID',
           as: 'comments._likes'
       },
    },
    //lookup in the user that wrote the comment
    {
       $lookup: {
           from: 'users',
           localField: 'comments.userID',
           foreignField: '_id',
           as: 'comments.user'
       },
   },
     {
        $set: {
           'comments.likes':'$comments._likes.userID',
            'likes': '$_likes.userID'
        }
    }, 
    
    {
        $project: {
    /////////FILTER NOT WORKING////////////
        //   'comments': {
        //         $filter: { input: "$comments", as: "cm", cond: { $ifNull: ["$$cm._id", false] } }
        //       } , //returns an error
           'comments.userID':0,
           'comments.user.password':0,
           'comments._likes':0,
           'userID':0,
           'user.password':0,
            'commentsID':0,
            '_likes': 0,
             
         }

    },
    {
       
           $group: {
             _id : "$_id",
             user:{$first:'$user'},
             likes:{$first:'$likes'},
             date:{$first:'$date'},
             content:{$first:'$content'},
             comments: { $push: "$comments" },
           }
    }
]

thank you for you help!

comments

 [
   {
    _id: ObjectId("com1"),
    userID:ObjectId("eliran1"),
    content: 'comment 1',
    date: 2022-03-02T22:55:16.224Z,
    
  },
  {
    _id:ObjectId("com2"),
    userID: ObjectId("eliran1"),
    content: 'comment 2',
    date: 2022-03-05T18:34:52.890Z,
    __v: 0
  }
]

posts

[
 {
    _id: new ObjectId("post1"),
    userID: new ObjectId("eliran1"),
    content: 'post 1',
    commentsID: [],
    date: 2022-03-05T18:28:11.487Z,
  },
  {
    _id: new ObjectId("post2"),
    userID: new ObjectId("shira1"),
    content: 'post 2',
    commentsID: [ObjectId("com1"),
                  ObjectId("com2") ],
    date: 2022-03-05T18:34:46.364Z,
  }
]

users

[
 {
    _id: new ObjectId("eliran1"),
    user: 'eliran222',
    password: '123456789',
    email: 'fdfd@fdfd.com33',
    gender: true,
    __v: 0
  },
  {
    _id: new ObjectId("shira1"),
    user: 'shira3432',
    password: '123456789',
    email: 'fdrf@gfge.com',
    gender: false,
  }
]

likes

[
 {
    _id: ObjectId("like1"),
    userID: ObjectId("eliran1"),
    postCommentID:ObjectId("post1"),
  },
  {
    _id:ObjectId("like2"),
    userID: ObjectId("shira1"),
    postCommentID:ObjectId("com1"),
  }
]

expected results:

[
 {
    _id: new ObjectId("post1"),
    user: {
            user: 'eliran222',
            email: 'fdfd@fdfd.com33',
             gender: true,
  },
    content: 'post 1',
    comments: [],
    date: 2022-03-05T18:28:11.487Z,
    likes:[{/*eliran`s user*/}]
  },
  {
    _id: new ObjectId("post2"),
    userID: {
   _id: new ObjectId("shira1"),
    user: 'shira3432',
    email: 'fdrf@gfge.com',
    gender: false,
},
    content: 'post 2',
    comments: [{
    _id: ObjectId("com1"),
    user:{
            _id:objectId(eliran1)
            user: 'eliran222',
            email: 'fdfd@fdfd.com33',
             gender: true,
         },
    content: 'comment 1',
    date: 2022-03-02T22:55:16.224Z,
    likes:[{/*shira`s user*/}]
    
  },
  {
    _id:ObjectId("com2"),
    user: { 
            objectId(eliran1)
            user: 'eliran222',
            email: 'fdfd@fdfd.com33',
             gender: true,
       },
    content: 'comment 2',
    date: 2022-03-05T18:34:52.890Z,
    likes:[]
  }],
    date: 2022-03-05T18:34:46.364Z,
  }
]
}
]




ELEVY
  • 3
  • 3
  • it can help if you give sample data, and expected output, else its hard to make a query, and not possible to test it – Takis Mar 05 '22 at 21:39
  • I don't think you understand MongoDB as a concept, you shouldn't have so many collections depending on so many others, why are likes not in posts and users? comments should be in posts with the user attributes to be shown on a page... this is not a relational database – balexandre Mar 05 '22 at 23:33
  • i built it that way because i wanted be able to quary list of liked comments or posts more easily. – ELEVY Mar 06 '22 at 00:04
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Mar 07 '22 at 00:58

0 Answers0