1. Home
  2. MongoDB
  3. MongoDB Tutorial
  4. MongoDB – Create Collection

MongoDB – Create Collection

MongoDB Create Collection

MongoDB stores documents in collections. The collections are analogous to tables in relational databases like MySQL. Generally, you don’t need to create a collection, MongoDB does this automatically during insertion of the first document.

Syntax

Use db.createCollection() command to create collection. You need to specify the name of collection, Also can provide some optional options like memory size and indexing.

> db.createCollection(name, options)   

MongoDB – Create Collection

Login to Mongo shell and select your database with the following command.

> use mydb

Now create your collection with createCollection() command. The below command will create collection named mycol in current database.

> db.createCollection("TECADMIN")  

{ "ok" : 1 }
Was this article helpful to you? Yes 5 No 1
Advertisement