In this bankruptcy, we will see how to create a group the use of MongoDB.
The createCollection() Method
MongoDB db.CreateCollection(name, options) is used to create collection.
Syntax
Basic syntax of createCollection() command is as follows −
db.createCollection(name, options)
In the command, name is name of collection to be created. Options is a record and is used to specify configuration of series.
Parameter | Type | Description |
---|---|---|
Name | String | Name of the collection to be created |
Options | Document | (Optional) Specify options about memory size and indexing |
Options parameter is optional, so that you need to specify most effective the name of the gathering. Following is the listing of alternatives you can use −
Field | Type | Description |
---|---|---|
capped | Boolean | (Optional) If true, enables a capped collection. Capped collection is a fixed size collection that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you need to specify size parameter also. |
autoIndexId | Boolean | (Optional) If true, automatically create index on _id field.s Default value is false. |
size | number | (Optional) Specifies a maximum size in bytes for a capped collection. If capped is true, then you need to specify this field also. |
max | number | (Optional) Specifies the maximum number of documents allowed in the capped collection. |
While putting the document, MongoDB first assessments length subject of capped collection, then it checks max discipline.
Examples
Basic syntax of createCollection() method without alternatives is as follows −
>use test
switched to db test
>db.createCollection("mycollection")
{ "ok" : 1 }
>
You can take a look at the created series by using using the command display collections.
>show collections
mycollection
system.indexes
The following example suggests the syntax of createCollection() method with few important alternatives −
> db.createCollection("mycol", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } ){
"ok" : 0,
"errmsg" : "BSON field 'create.autoIndexID' is an unknown field.",
"code" : 40415,
"codeName" : "Location40415"
}
>
In MongoDB, you don't want to create series. MongoDB creates collection routinely, whilst you insert a few document.
>db.tutorialspoint.insert({"name" : "tutorialspoint"}),
WriteResult({ "nInserted" : 1 })
>show collections
mycol
mycollection
system.indexes
tutorialspoint
>