C100DBA · Question #77
Which of the following operator can be used to limit the number of documents in an array field of a document after an update is performed?
The correct answer is B. $push along with $each, $sort and $slice. $push combined with the $each, $sort, and $slice modifiers is the correct MongoDB approach for controlling array size during an update - $slice specifically caps the number of elements kept in the array (positive value keeps from the start, negative from the end). $arrayLimit (op
Question
Which of the following operator can be used to limit the number of documents in an array field of a document after an update is performed?
Options
- A$arrayLimit
- B$push along with $each, $sort and $slice
- C$removeFromSet
- DNone of the above
How the community answered
(44 responses)- A9% (4)
- B84% (37)
- C2% (1)
- D5% (2)
Explanation
$push combined with the $each, $sort, and $slice modifiers is the correct MongoDB approach for controlling array size during an update - $slice specifically caps the number of elements kept in the array (positive value keeps from the start, negative from the end). $arrayLimit (option A) does not exist in MongoDB; it is a fabricated operator. $removeFromSet (option C) is also fictitious - the real operator for removing elements from an array is $pull or $pop, neither of which limits size post-update. Option D falls away because a valid solution does exist.
Memory tip: Think of the combo as a pipeline in one operator - Enter elements ($each), Sort them ($sort), then Slice to size ($slice) - ESS rides inside $push.
Topics
Community Discussion
No community discussion yet for this question.