MongoDB.live, free & fully virtual. June 9th - 10th. Register Now MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Store more documents in collection

Hi all!
I am trying to store documents in a collection which is coming from an api request. The size to total documents is 500 but in my mongodb it is saving only 50 of them. I tried to use createColletion method with the size of 500 but still saving 50 documents. How can I store all of 500 documents in the collection?

Can you post the code you are using?

:wave: Hi @Ali_Abbas and welcome to the community.

It sounds like you’re trying to do something like the following:

db.createCollection( "myColl", { size: 500, ... } );

This is just an assumption however since you didn’t provide any code examples.

One thing to note is that size is the size in bytes for a capped collection. This has no meaning for a regular collection.

The following comes from the db.createCollection() documentation for the size key:

Optional. Specify a maximum size in bytes for a capped collection. Once a capped collection reaches its maximum size, MongoDB removes the older documents to make space for the new documents. The size field is required for capped collections and ignored for other collections.

If you’re trying to create a capped collection, then you will need to determine your average document size and then multiply that by 500 to get an approximate value for size to hold the 500 documents. You can combine the size key with the max key to make sure you don’t have more than 500 documents in the collection, but you can’t guarantee a minimum number of documents unless you were to oversize the collection.

If you’re trying to create a regular collection then you don’t need to do anything special to store 500 documents in the collection. Just start writing to it and you will be able to store as many documents as you have space available on the hard drive.

If I’ve misunderstood what you were asking, please provide more information so that we can provide better answers.

3 Likes