MongoDB Create Collection

MongoDB Create Collection – In this MongoDB Tutorial, we shall learn ways to create a MongoDB Collection. There are both implicit and explicit ways to create a collection.

Implicitly Create Collection

To create a new collection in MongoDB implicitly, follow below steps :

Step 1: Select a MongoDB database you like to Create a Collection within, using USE command. Following is the syntax of USE command.

use <database_name>

Step 2: Insert a record to Collection, with Collection Name mentioned in the command as shown below

db.<collection_name>.insert(<document>)

The collection should be created.

Step 3: View the existing collections using following command.

show collections
ADVERTISEMENT

Example 1 – Create a MongoDB Collection

Following is an example where we shall try creating a collection namedcustomers  intutorialkart  database.

Open Mongo Shell and follow the commands in sequence.

> use tutorialkart
switched to db tutorialkart
> show collections
> db.customers.insert({ name: "Honey", age: 25, cars: [ "Audi R8" ] })
WriteResult({ "nInserted" : 1 })
> show collections
customers
>

Following is the explanation for each mongodb command we executed above

  1. use tutorialkart  switched to tutorialkart database.
  2. show collections  lists the collections in the selected database. There are no collections at this moment.
  3. db.customers.insert()  makes a check if the collection is present. As collection is not present yet, it creates one with the namecustomers  specified in the command.
  4. show collections  now listscustomers  collection.

Explicitly Create Collection

To create a new collection in MongoDB explicitly, follow below steps :

Step 1: Select a MongoDB database you like to Create a Collection within, using USE command. Following is the syntax of USE command.

use <database_name>

Following is the syntax of createCollection() command that creates a new collection.

db.createCollection(name, options)

where

name[mandatory] name of collection
options[optional] mongodb document specifying information about collection

Options

FieldTypeDescription
cappedBooleanIf true then collection is limited in size. i.e., Number of documents that could be stored in the collection is limited.
autoIndexIdBoolean[depreciated]
sizeNumberSize of Collection in Bytes
maxNumberNumber of MongoDB Documents that could be stored in the collection

Note#1 : All fields in Options are optional.

Note#2 : If you specify capped as true, then you should specify size.

Note#3 : When a new Document arrives Collection for insertion, size and max are the prerequisites that are verified. If the collection comes to the threshold of any, old documents are overwritten in a round robin fashion.

View the existing collections using following command.

show collections

Example 2 – Create a MongoDB Collection

Following is an example where we shall try creating a collection namedmyNewCollection   intutorialkart  database.

Open Mongo Shell and follow the commands in sequence.

> use tutorialkart
switched to db tutorialkart
> show collections
customers
> db.createCollection("myNewCollection", { capped : true, size : 280000, max : 1000 } )
{ "ok" : 1 }
> show collections
customers
myNewCollection
>

Following is the explanation for each mongodb command we executed above

  1. use tutorialkart  switched to tutorialkart database.
  2. show collections  listed the collections in the selected database. There is only a collection namedcustomers .
  3. db.createCollection()  created new collection namedmyNewCollection .
  4. show collections  now listscustomers  collection.

Conclusion

In this MongoDB Tutorial – MongoDB Create Database, we have learnt to create a database using an example scenario.