First Select Or Create a database.
> use mydb
switched to db mydb
Using db.createCollection("yourCollectionName")
method you can explicitly create Collection.
> db.createCollection("newCollection1")
{ "ok" : 1 }
Using show collections
command see all collections in the database.
> show collections
newCollection1
system.indexes
>
The db.createCollection()
method has the following parameters:
Parameter | Type | Description |
---|---|---|
name | string | The name of the collection to create. |
options | document | Optional. Configuration options for creating a capped collection or for preallocating space in a new collection. |
The fllowing example shows the syntax of createCollection()
method with few important options
>db.createCollection("newCollection4", {capped :true, autoIndexId : true, size : 6142800, max : 10000})
{ "ok" : 1 }
Both the db.collection.insert()
and the db.collection.createIndex()
operations create their respective collection if they do not already exist.
> db.newCollection2.insert({name : "XXX"})
> db.newCollection3.createIndex({accountNo : 1})
Now, Show All the collections using show collections
command
> show collections
newCollection1
newCollection2
newCollection3
newCollection4
system.indexes
If you want to see the inserted document, use the find()
command.
> db.newCollection2.find()
{ "_id" : ObjectId("58f26876cabafaeb509e9c1f"), "name" : "XXX" }