DEV Community

Donald Feury
Donald Feury

Posted on • Originally published at donaldfeury.xyz on

1 1

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:

db.createCollection("podcasts")

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More