0
0
MongoDBquery~5 mins

Array expressions ($size, $arrayElemAt, $filter) in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the $size array expression do in MongoDB?

The $size expression returns the number of elements in an array.

It helps you count how many items are inside an array field.

Click to reveal answer
beginner
How does $arrayElemAt work in MongoDB?

$arrayElemAt returns the element at a specific position in an array.

Positions start at 0, so $arrayElemAt: [array, 0] gives the first item.

Click to reveal answer
intermediate
Explain the purpose of $filter in MongoDB array expressions.

$filter lets you select only the elements in an array that match a condition.

It creates a new array with just the items you want.

Click to reveal answer
intermediate
What happens if you use $size on a field that is not an array?

If the field is not an array (including null or missing), $size throws an error.

Click to reveal answer
advanced
How can $filter be combined with $size to count filtered elements?

You can use $filter to create a smaller array with only matching elements, then use $size to count how many matched.

Click to reveal answer
What does $size return when used on an array?
AThe last element of the array
BThe number of elements in the array
CA filtered version of the array
DThe first element of the array
Which expression would you use to get the third element of an array?
A<code>$filter</code>
B<code>$size</code>
C<code>$arrayElemAt</code> with index 2
D<code>$arrayElemAt</code> with index 3
What does $filter do in MongoDB?
ASelects elements matching a condition
BReturns an element at a position
CCounts elements in an array
DSorts the array elements
What will happen if $size is used on a non-array field?
AReturns 0
BReturns null
CReturns 1
DThrows an error
How can you count only the elements in an array that meet a condition?
AUse <code>$filter</code> then <code>$size</code>
BUse <code>$arrayElemAt</code> with <code>$size</code>
CUse <code>$size</code> alone
DUse <code>$filter</code> alone
Describe how to use $filter to select elements from an array based on a condition.
Think about how you pick only certain items from a list.
You got /3 concepts.
    Explain how $arrayElemAt and $size can be used together in a query.
    One picks an item, the other counts items.
    You got /3 concepts.