How to create a collection in MongoDB
Donald Feury
Posted on August 12, 2021
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 createCollection
method.
If you wanted to create a new collection called podcasts
, do the following:
db.createCollection("podcasts")
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
💖 💪 🙅 🚩
Donald Feury
Posted on August 12, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.