Recall & Review
beginner
What does the
$size operator do in MongoDB?The
$size operator returns the number of elements in an array field in a document.Click to reveal answer
beginner
How do you use
$size in a MongoDB query to find documents where an array has exactly 3 elements?Use
{ arrayField: { $size: 3 } } in the query to find documents where arrayField has exactly 3 elements.Click to reveal answer
intermediate
Can
$size be used to find arrays with length greater than or less than a number?No,
$size only matches arrays with an exact length. For greater or less than, use $expr with $gt or $lt and $size inside.Click to reveal answer
beginner
Example: Find documents where the
tags array has 2 elements.Query:
{ tags: { $size: 2 } } will return documents where the tags array length is exactly 2.Click to reveal answer
beginner
Why is
$size useful in real life?It helps find documents with arrays of a specific length, like users with exactly 3 phone numbers or orders with 5 items.
Click to reveal answer
What does
{ items: { $size: 4 } } find in MongoDB?✗ Incorrect
$size matches arrays with exact length, so it finds arrays with exactly 4 elements.Can
$size be used to find arrays with length greater than 5?✗ Incorrect
$size only matches arrays with exact length. For greater than, use $expr with $gt.Which query finds documents with an array 'scores' of length 3?
✗ Incorrect
$size is the correct operator to match array length exactly.What type of MongoDB field does
$size work on?✗ Incorrect
$size works only on array fields to check their length.If a document has
tags: ['a', 'b', 'c'], which query matches it?✗ Incorrect
The array has 3 elements, so
$size: 3 matches.Explain how the
$size operator works in MongoDB and give an example query.Think about how to find documents with arrays of a certain length.
You got /3 concepts.
Describe a situation where
$size is useful and how you would write a query for it.Consider arrays like tags, phone numbers, or items in orders.
You got /3 concepts.