Tutorials References Exercises Videos Menu
Create Website Get Certified Upgrade

MongoDB mongosh Create Collection


Create Collection using mongosh

There are 2 ways to create a collection.

Method 1

You can create a collection using the createCollection() database method.

Example

db.createCollection("posts")
Try it Yourself »

Method 2

You can also create a collection during the insert process.

Example

We are here assuming object is a valid JavaScript object containing post data:

db.posts.insertOne(object)
Try it Yourself »

This will create the "posts" collection if it does not already exist.

Remember: In MongoDB, a collection is not actually created until it gets content!