Home » MongoDB » How to Create Collections in MongoDB?

How to Create Collections in MongoDB?

In MongoDB, collections are like tables and used to store the documents. Below are the two examples to create the collections in MongoDB by using the command prompt in Windows and by using the Compass GUI tool for MongoDB.

MongoDB Create Collection Examples

Using MongoDB command line interface.

  1. Open the command prompt.
  2. Change the directory to MongoDB, for example, C:\mongodb\server\4.0\bin.
  3. Then start the MongoDB by giving the following command.
mongo
  1. Open the database.
use scott

Output:

switched to db scott
  1. Then create a collection using createCollection() method, as shown in the below example.
db.createCollection('employees');

Output:

{ "ok" : 1 }

Your collection employees have been created.

Create Collections Using MongoDB Compass

  1. Open the MongoDB Compass.
  2. Connect to it.
  3. On the left side, select the database from the list.
  4. Then on the right side, click on the Create Collection button.
  5. A Create Collection window will appear.
  6. Give the collection name into the Collection Name field, as shown in below image.
  7. Then click on the Create Collection button to create it.

Create collection in mongodb

See also: