How do I create a capped array in MongoDB?

1.3k Views Asked by At

Are capped arrays created automatically everytime you add an array to a document, or is there a command you must enter?

Here is the command for capped collections:

db.createCollection( "log", { capped: true, size: 100000 } )

From what I read, you only have to use the $slice command. Can anyone confirm this?

Thanks

1

There are 1 best solutions below

0
On

Try using the $slice operator for your capped array, as explained in this post about MongoDB 2.4.

Such as with $slice: [capValue] in your query.

Mongo maintains the array in natural order and trims the array to 5 elements. It is possible to specify to slice from the start of the array or the end of the array by using positive or negative integers with $slice.

Hope that helps!