We know that the data in MongoDB is stored in form of documents. These documents are stored in Collection and Collection is stored in Database.

Two ways to Create a Collection
Here are two ways of creating collections:
- You can create a collection on the fly when inserting a document (using the
insert()method. - You can also create a collection explicitly, using the
createCollection()method.
Definition
db.createCollection(name, options)
Creates a new collection or view.
Because MongoDB creates a collection implicitly when the collection is first referenced in a 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.
db.createCollection() is a wrapper around the database command create.
The db.createCollection() method has the following prototype form: Lets see the options that we can provide while creating a collection:
capped: type: boolean.
This parameter takes only true and false. This specifies a cap on the max entries a collection can have. Once the collection reaches that limit, it starts overwriting old entries.
The point to note here is that when you set the capped option to true you also have to specify the size parameter. Learn more skills from MongoDB Certification
size: type: number.
This specifies the max size of collection (capped collection) in bytes.
max: type: number.
This specifies the max number of documents a collection can hold.
autoIndexId: type: boolean
The default value of this parameter is false. If you set it true then it automatically creates index field _id for each document.
db.createCollection( <name>,
{
capped: <boolean>,
autoIndexId: <boolean>,
size: <number>,
max: <number>,
storageEngine: <document>,
validator: <document>,
validationLevel: <string>,
validationAction: <string>,
indexOptionDefaults: <document>,
viewOn: <string>, // Added in MongoDB 3.4
pipeline: <pipeline>, // Added in MongoDB 3.4
collation: <document>, // Added in MongoDB 3.4
writeConcern: <document>
}
)
The created user can execute db.createCollection() on the specified databases.
Alternatively, you can add the required roles to an existing user using db.grantRolesToUser()
Create a Collection with Document Validation
Collections with validation compare each inserted or updated document against the criteria specified in the validator option. Depending on the validationLevel and validationAction, MongoDB either returns a warning, or refuses to insert or update the document if it fails to meet the specified criteria.
db.createCollection( "contacts", {
validator: { $jsonSchema: {
bsonType: "object",
required: [ "phone" ],
properties: {
phone: {
bsonType: "string",
description: "must be a string and is required"
},
email: {
bsonType : "string",
pattern : "@mongodb\.com$",
description: "must be a string and match the regular expression pattern"
},
status: {
enum: [ "Unknown", "Incomplete" ],
description: "can only be one of the enum values"
}
}
} }
} )
To get in-depth knowledge, enroll for a live free demo on MongoDB online Training