Zeokav
Zeokav

Reputation: 1703

Mongodb limit number of records in collection

I want to store my data in a collection that holds a maximum of ~10000 past records before they're deleted automatically from the database, like a FIFO queue of 10k records.

I have been looking at capped collections, but apart from the maximum number of records, they also require the maximum size of the collection. Now, I don't really know how much size 10k records are going to occupy. If I set a size that can't hold 10k records, I'll have lesser than I need. On the other hand, if I set an upper limit on the size, space is going to be wasted because mongo allocates the space beforehand.

What I can do is get dummy records, but I don't know how to check the size of each document.

Does anyone know of a method that can set an upper limit solely on the number of documents in my collection? Using the latest version on mongo out right now (v 3.6.3)

Upvotes: 1

Views: 1067

Answers (1)

Kevin Smith
Kevin Smith

Reputation: 14456

I'd personally push in 10000 actual real documents into a mongodb collection then call stats on the collection

db.test.stats()

Then use the size property:

> db.test.stats()
{
    "ns" : "test.test",
    "size" : 28609,
    "count" : 1018,
    "avgObjSize" : 28,

You may also want to add some padding, say 10% to that number:

cap col size = size * 1.1

Upvotes: 1

Related Questions