How to create a collection in MongoDB
For a full overview of MongoDB and all my posts on it, check out my overview.
If you want to explicitly create a new collection in MongoDB rather than letting it be automatically created when an insert operation happens, you can do so using the
If you wanted to create a new collection called
podcasts, do the following:
createCollections can also take a second argument of a document describing a large number of options that can modify the collection. You can read up on them here but some of the more useful options include:
- Set a TTL for documents to be automatically removed after a set time has passed
- Set a maximum size for the collection
- Create validation rules to control what documents can exist in the collection
Did you find this information useful? If so, consider heading over to my donation page and drop me some support.
Want to ask a question or just chat? Contact me here