MongoDB: Creating a Collection

Welcome to a tutorial on MongoDB. Here you will learn how to create a collection. 

A collection is automatically created when it is referenced in any command in MongoDB. Check out the example below where you run an insert command.

db.student.insert({
	name: "Ada Chang"
})

 

The above command creates a collection named student if it doesn't exist already in the database. However, we can explicitly create a collection using the createCollection() command. Check out the syntax of createCollection method below:

db.createCollection(name, options)

 

Now, from the above command, the name will be the name of the collection and options is a document that can be used to specify configurations for the collection.

In addition, the options parameter is optional, and you can simply create a collection by simply providing a name for the collection. However, if you want to configure your collection, there are different options available to do that.

The table shows the available configuration options for creating a collection in MongoDB:

FieldTypeDescription
cappedboolean(Optional) It is used to create a capped collection, wherein we specify the maximum size or document counts to prevent it from growing beyond a set maximum value.
sizenumber(Optional) It is used to specify the maximum size in bytes for a capped collection. If a collection is capped and reaches its maximum size limit, MongoDB then removes older documents from it to make space for new ones.
maxnumber(Optional) It can be used to specify the maximum number of documents allowed in a capped collection.
validatordocument(Optional) It validates any document inserted or updated against provided validation rules.
validationLevelstring

(Optional) It is used to define how strictly validation rules must be applied to existing documents during an update.

Available values include:

off: No validation for inserts or updates.

Strict: It is the default value for this option. Also, it instructs MongoDB to apply validation rules to all inserts and updates.

Moderate: It is used when we want to apply validation rules to inserts and updates on only the existing valid documents and not on the existing invalid documents.

validationActionstring(Optional) It is can be used to set the action to be taken upon validation i.e. if any document fails the set validation, then whether to show an error or just warn about the violations, thus default value is an error.

 

MongoDB: Creating a Capped Collection

We can create a capped collection by making use of the command below.

db.createCollection("student", { capped : true, size : 5242880, max : 5000 } )

The above command creates a collection named student, with a maximum size of 5 megabytes and a maximum of 5000 documents.

 

MongoDB: Drop a Collection

In MongoDB, any collection in a database can be dropped easily by making use of the command below.

db.collection_name.drop()

drop() method returns true. if the collection is dropped successfully, else it will return false.