9951 explained code solutions for 126 technologies


mongodbHow to create a collection in MongoDB?


Creating a collection in MongoDB is a simple process. To create a collection, use the db.createCollection() method. The syntax for this method is as follows:

db.createCollection(name, options)
  • name: The name of the collection to be created.
  • options: This is an optional parameter. It is a document that specifies the configuration of the collection.

Example

db.createCollection("customers", { capped : true, autoIndexId : true, size : 6142800, max : 10000 } )

Output example

{ "ok" : 1 }

Helpful links

Edit this code on GitHub