Getting following error

Sample code below

“num_favs” : { $cond: { if: { $isArray: “$commonCast” }, then: { $size: “$commonCast” }, else: 0} }
.
.
.
“$match”:{
“num_favs”: 1
}

Gives the following error

$in requires an array as a second argument, found: missing

You got an error about the $in operator but you post the part of a pipeline that does not show the $in operator. We cannot help if we do not see how you use it.

That is exactly my problem, I am also not able to decipher the issue, I can share the complete code if you want ?

Am Attaching the code:

[{$match: {
  "tomatoes.viewer.rating" : {$gte: 3}
}}, {$addFields: {
  		"USRelease": {
				$in: [ "USA","$countries"]
			}
			, "ratings":"$tomatoes.viewer.rating"
			,"commonCastT": {
				$setIntersection: [ "$cast", [
				  "Frank Powell",
                "Grace Henderson",
				"Mary Pickford",
                "Gladys Egan"]
				  ]
			}
			,commonCast: {
				$setIntersection: [ "$cast", [
				  "Sandra Bullock",
				  "Tom Hanks",
				  "Julia Roberts",
				  "Kevin Spacey",
				  "George Clooney"]
				  ]
			}
			,"num_favs" : { $cond: { if: { $isArray: "$commonCast" }, then: { $size: "$commonCast" }, else: 0} }
			,"num_favs_found" : { $isArray: "$commonCast" }
			,"found_type":{ $type: "num_favs" }
}}, {$match: {
  			"USRelease": true
}}, {$addFields: {
  			"commonCast": {
			  "$cond": {
				"if": {
				  "$ne": [ { "$type": "$commonCast" }, "array" ]
				},
				"then": [],
				"else": "$commonCast"
			  }
			}
  		,	"commonCastT": {
			  "$cond": {
				"if": {
				  "$ne": [ { "$type": "$commonCastT" }, "array" ]
				},
				"then": [],
				"else": "$commonCastT"
			  }
			}

}}, {$project: {
  		"_id":0
			, "title" : 1
			, cast : 1
			, "ratings": 1
			, countries: 1	
			, "USRelease": 1
			, "inCast": 1
			, commonCast: 1
			, "num_favs" : 1
			, "num_favs_found" : 1
			, "found_type": 1
			, "commonCastT": 1
			, num_favsT : 1
			, num_favsT_type : 1
			, "xyz": 1

}}, {$addFields: {
  		num_favsT : { $size: "$commonCastT"} 
  		, num_favs : { $size: "$commonCast"} 
}}, {$addFields: {
  num_favsT_type : { $type: "$num_favsT" }
}}, {$sort: {
  num_favsT: -1
}}]

if a document has no field named countries your $in will failed with the given error because, well, countries, the second argument of $in, is missing.

1 Like