Ticket: User Report with 'join'

i tried to complete this aggrigation pipeline query with ‘join’ , but i am getting a wierd results

const pipeline = 

          [

            {

              '$lookup': {

                'from': 'movies', 

                'let': {

                  'movie_id': '$movie_id'

                }, 

                'pipeline': [

                  {

                    '$match': {

                      '$expr': {

                        '$eq': [

                          '$_id', '$$movie_id'

                        ]

                      }

                    }

                  }, {

                    '$count': 'count'

                  }

                 

                ], 

                'as': 'Comments_Per_movie'

              }

            }, {

              '$sort': {

                'Comments_Per_movie': -1

              }

            },{$limit:20}

          ]

and i’m getting 20 documents look like this:

  1. _id: 5a9427648b0beebeb6957ba5
  2. date:1983-02-24T17:57:36.000+00:00
  3. Comments_Per_movie:
    Array
    0:Object
    count:1
  4. name:“Shawn Mccormick”
  5. email:"shawn_mccormick@fakegmail.com"
  6. movie_id:573a1391f29313caabcd8543
  7. text:“Quisquam doloremque odio nostrum enim accusantium deleniti. Itaque mag…”

Do i missising here something? i supposed to get an array with count:[200-300] here

It’s not as complicated as you’ve made it. $lookup is not necessary.

Suggest you look at the test script to see what fields it’s expecting. That’s the way to get through these labs.

1 Like

i realized i could done it with just operation on the comments collection ,
but i don’t understand why the aggregation i done here does not get the same results

Have you taken the M201 Aggregation Framework course? Look into the documentation for $lookup.

Nope , i will take a look , just asked for a quick answer here
thanks

Hi @arik_62314,

You may want to expressive lookup. We need to use pipeline stages for sorting the comments.

Kanika