db.createCollection()

createCollection(name, options?)

Note:

MongoDB creates a collection implicitly when the collection is first referenced in command, this method is used primarily for creating new collections that use specific options.

For example, you use db.createCollection() to create a capped collection or to create a new collection that uses document validation.


Create Simple Collection:

db.createCollection('products1')

{ "ok" : 1 }


Create Capped Collection:

Note: 

For capped collection size is required at the time of collection creation.

Example 1: When size is not provided

db.createCollection('products',{capped:true})

{ "ok" : 0, "errmsg" : "the 'size' field is required when 'capped' is true", "code" : 72, "codeName" : "InvalidOptions" }


Example 2: When the size is provided.

db.createCollection('products',{capped:true,size:100})

{ "ok" : 1 }


No comments:

Post a Comment