Checking if setIntersection is null

I am getting an error, as $size must be activated on an array, but it seems that $setIntersection returns null in some cases.

my code:

    $match: {
      "countries": "USA",
      "tomatoes.viewer.rating": {
        $gte: 3
    $addFields: {
      num_favs: {
        $size: {
          $setIntersection: [
              "Sandra Bullock",
              "Tom Hanks",
              "Julia Roberts",
              "Kevin Spacey",
              "George Clooney"

How should I approach this?

Hi @Harel_Shattenstein,
As per the $setIntersection documentation:

If no intersections are found (i.e. the input arrays contain no common elements), $setIntersection returns an empty array.

So it should not return an empty array, can you please post a screenshot of the error that you are getting?

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

1 Like

If all arguments of $setIntersection are arrays then the result is an array.

If some arguments are not arrays then $setIntersection will be null. Since the first argument of is an hard coded array, then some documents probably do not have $cast as an array.

So you $setIntersection is null, then you get the error message about $size.