Capped collection

How do you create a mongo caped collection with Meteor?

If there is no direct way, what would be an alternative method to ensure a collection does not exceed X number of documents, and to remove from the beginning when inserting to ensure the cap remains?

1 Like

Do you need to limit the number of elements in the database, or do you just want to limit what you display to the user?

Limiting the elements you see can be done the same way you would manage paging. Have a look at this.

If you really want to limit the number of records in the mongodb, then you could use https://atmospherejs.com/matb33/collection-hooks and apply an after.insert() hook to remove the oldest items once you have reached your cap.

Hi @hanskohls

is the collection-hooks deleted because it reached maximum number of records, or deleted after, say 1 year? I want to delete, say 1 year(or some other expiration date).

I’d use a cron job to go over the collection at daily intervals for that. There are several cron libraries for Meteor and npm. I’ve not used any of them myself though.