Kabocha Porter
Kabocha Porter

Reputation: 301

$size with $in as parameter in mongodb

I have the following query, the error is "\t\"errmsg\" : \"$size needs a number\",", basically I cannot use $in as parameter, why is that? Isn't $in feeding the number if there is a match? I am also having a hard time to solve this, if I use another $or as commented, I get a warning that I cannot use two $or consecutively.

db.Movies.find({
    "company.country": {$in: ["Brazil", "Mexico","USA"]},
     $or: [{"company.start_year": {$gte: 1990, $lte: 1995}}, {"company.start_year": {$gte: 2005, $lte:2010}}],
     actors: {$size: {$in: [2, 4, 6, 8]}},
    //{$or: [{actors: {$size: 2}}, {actors: {$size: 4}}, {actors: {$size: 8}}]},
     ratings: {$gte: 7}
}, {_id: 0, movie_name: 1, })

Upvotes: 0

Views: 331

Answers (1)

Wernfried Domscheit
Wernfried Domscheit

Reputation: 59553

You have to use the $expr operator. Try this one:

{ $expr: { $in: [{ $size: "$actors" }, [2, 4, 6, 8]] } }

If you prefer the $or then use this one:

{
   "company.country": { $in: ["Brazil", "Mexico", "USA"] },
   $and: [
      { $or: [{ "company.start_year": { $gte: 1990, $lte: 1995 } }, { "company.start_year": { $gte: 2005, $lte: 2010 } }] },
      { $or: [{ actors: { $size: 2 } }, { actors: { $size: 4 } }, { actors: { $size: 8 } }] }
   ],
   ratings: { $gte: 7 }
}

Upvotes: 2

Related Questions