Issue on Lab: Using Cursor-like Stages

What is the title of the 25th film in the aggregation result?
Here is my pipeline, I’m not sure where I’m going wrong!

[
  {
    '$match': {
      'cast': {
        '$elemMatch': {
          '$exists': true
        }
      }, 
      'countries': {
        '$in': [
          'USA'
        ]
      }, 
      'tomatoes.viewer.rating': {
        '$gte': 3
      }
    }
  }, {
    '$project': {
      'title': 1, 
      'num_favs': {
        '$size': {
          '$setIntersection': [
            '$cast', [
              'Sandra Bullock', 'Tom Hanks', 'Julia Roberts', 'Kevin Spacey', 'George Clooney'
            ]
          ]
        }
      }
    }
  }, {
    '$match': {
      'num_favs': {
        '$ne': 0
      }
    }
  }, {
    '$sort': {
      'num_favs': -1, 
      'tomatoes.viewer.rating': -1, 
      'title': -1
    }
  }, {
    '$skip': 24
  }
]

What do you get? An error, the wrong result or no result at all?

The title name I get for the above pipeline it’s not matching (wrong answer)

I fixed it by changing $project to $addFields and by removing the title attribute

... }, {
    '$addFields': {
      'num_favs': {
        '$size': {
          '$setIntersection': [
            '$cast', [
              'Sandra Bullock', 'Tom Hanks', 'Julia Roberts', 'Kevin Spacey', 'George Clooney'
            ]
          ]
        }
      }
    }
  }, {
    '$match': { ...

Glad you found a solution.

The root cause of having the wrong title was that your $project did not kept the field tomatoes.viewer.rating. So later your $sort was not producing the appropriate order since the field was missing.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.