$size

noun

say it: “DOL-er SYZE” /ˈdɑlər ˈsaɪz/

A MongoDB filter operator that matches an array with exactly a given number of items.

What does $size do in a MongoDB filter?

$size matches documents whose array field has exactly the number of items you give. It takes one exact number, not a range such as "more than 5".

db.recipes.find({ ingredients: { $size: 3 } }, { _id: 0, name: 1 });
// [ { name: 'Garlic Toast' } ]

In a sentence

  • “The 'three-ingredient recipes' page is one query with $size 3.”
  • “$size can't say 'at least 3', so the app stores an ingredient count for that.”

Level: Apprentice · More MongoDB terms at this level

Updated