mongodb/mongo — pull request #181 by boivie (2012-02-25 11:58:50+00:00)
Title: SERVER-991: Added support for $trim

The $trim operator will limit the length of the arrays to which
it is applied. A positive number indicates how many items to keep
starting counting from the beginning of the array whereas a negative
number keeps items from the end of the array.

array: [1 2 3 4 5 6 7 8]
trim(5) -> keep 1 2 3 4 5, drop 6 7 8 -> [1, 2, 3, 4, 5]
trim(-5) -> drop 1 2 3, keep 4 5 6 7 8 -> [4, 5, 6, 7, 8]

$trim is special as it is allowed (and required) to reference the same
field as another update modifier. It can not be used by itself, but
the same effect can be used by doing a $pushAll: [] (pushing an empty
list) at the same time as using $trim.

$trim is always performed after updating the field with any other
modifier.

Test cases have been added.