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 document and is used to specify configuration of collection.
Options parameter is optional, so you need to specify only the name of the collection.
Following is the list of options you can use −
Examples
Basic syntax of createCollection() method without options is as follows −
>use school
switched to db school
>db.createCollection("mycollection")
{ "ok" : 1 }
>
You can check the created collection by using the command show collections.
>show collections
mycollection
system.indexes
The following example shows the syntax of createCollection() method with few important options −
>db.createCollection("mycol", { capped : true, autoIndexId : true, size :
6142800, max : 10000 } )
{ "ok" : 1 }
In MongoDB, you don't need to create a collection. MongoDB creates collection automatically, when you insert some document.
Capped collections are collections which can store data in the same order it is inserted.
you can't remove an individual object from the capped collection.
Capped collections can be used for logging, caching and auto-archiving.
Presently, the maximum size for a capped collection is 1e9(i.e. 1X109) for 32-bit machines. For 64 bit machines, there is no theoretical limit. Practically, it can be extended till your system resources permit.
Valid collection names
Collection names must begin with letters or an underscore.
A Collection name may contain numbers.
You can't use "$" character within the name of a collection. "$" is reserved.
A Collection name must not exceed 128 characters. It will be nice if you keep it within 80/90 characters.
Using a "." (dot) notation, collections can be organized in named groups. For example, tutorials.php and tutorials.javascript both belong to tutorials. This mechanism is called as collection namespace which is for user primarily. Databases don't have much to do with it.
In MongoDB, you don't need to create collection. MongoDB creates collection automatically, when you insert some document.
>db.codeforalgo.insert({"name" : "codeforalgo"})
>show collections
mycol
mycollection
codeforalgo
Comments
Post a Comment