MongoDB Documentation, Release 3.0.4 See also: 3.3.6 Limit Number of Elements in an Array after an Update Synopsis Pattern Note: When using the $sort modifier on the array element, access the field in the embedded document element directly instead of using the dot notation on the array field. After the operation, the document contains only the top 3 scores in the scores array: The following update uses the $push operator with: Consider the following document in the collection students : { _id: 1, scores: [ { attempt: 1, score: 10 }, { attempt: 2 , score:8 } ] } db.students.update( { _id: 1 }, { $push: { scores: { $each: [ { attempt: 3, score: 7 }, { attempt: 4, score: 4 } ], $sort: { score: 1 }, $slice: -3 } } } ) • the $each modifier to append to the array 2 new elements, • the $sort modifier to order the elements by ascending ( 1 ) score, and • the $slice modifier to keep the last 3 elements of the ordered array. Query Documents (page 96) New in version 2.4. Consider an application where users may submit many scores (e.g. for a test), but the application only needs to track the top three test scores. This pattern uses the $push operator with the $each , $sort , and $slice modifiers to sort and maintain an array of fixed size. 109 3.3. MongoDB CRUD Tutorials