DEV Community

Donald Feury
Donald Feury

Posted on • Originally published at on

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 createCollection method.

If you wanted to create a new collection called podcasts, do the following:


Enter fullscreen mode Exit fullscreen mode

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

Discussion (0)