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
Example 1 – Create a MongoDB Collection
Following is an example where we shall try creating a collection named customers in tutorialkart 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
- use tutorialkart switched to tutorialkart database.
- show collections lists the collections in the selected database. There are no collections at this moment.
- db.customers.insert() makes a check if the collection is present. As collection is not present yet, it creates one with the name customers specified in the command.
- show collections now lists customers 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
Field | Type | Description |
---|---|---|
capped | Boolean | If true then collection is limited in size. i.e., Number of documents that could be stored in the collection is limited. |
autoIndexId | Boolean | [depreciated] |
size | Number | Size of Collection in Bytes |
max | Number | Number 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 named myNewCollection in tutorialkart 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
- use tutorialkart switched to tutorialkart database.
- show collections listed the collections in the selected database. There is only a collection named customers .
- db.createCollection() created new collection named myNewCollection .
- show collections now lists customers collection.
Conclusion
In this MongoDB Tutorial – MongoDB Create Database, we have learnt to create a database using an example scenario.