Mongodb push to array but keep the array with a max of 3 items

Is there an easy way to push to an array in mongodb such that the array has no more than 3 items. If there are 3 items the push should throw out the first item in the array. I'm looking for a safe way to do this.

@muistooshort is correct.

The 2.4 $push, $each and $slice operators can be used to create a fixed length array.

You can then use the $sort operator to keep the array sorted and have the "last" item removed by the $push.

The documentation here has a great example.

There is a "structure type" called Capped array which is exactly made for this, which is new in 2.4 version. Here is exactly the documentation which describes the operation what you look for : DOCS