
In this chapter, we are going to understand the concept of collections in MongoDB. MongoDB has a method known as a collection method and is used to create a collection. It has the following syntax.
Syntax for createCollection () method
The following is the basic syntax for creating a collection in MongoDB:
> db.createCollection(name, options)In the above syntax, the ‘name’ refers to the name of the collection that we are going to create. And Options refers to a document that is used to specify the configuration of a collection.
1.
Name
String
It refers to the name of the collection which is to be created.
2.
Options
Document
It is an optional filed. Options are used to specify the options such as the memory size, indexing, etc.
The Options parameter is optional but the Name parameter is mandatory. Therefore, we always need to specify only the name of the collection and for options (if required); then we can specify any of the following from the list as shown below.
S No.
Field
Type
Description
1.
capped
Boolean
It is an optional field. If it is set to true, then it enables a capped collection. A Capped collection is a fixed size collection which can overwrite automatically its oldest entries upon reaching its maximum size. When it is set to true, then we need to specify the size of the parameter as well.
2.
autoIndexID
Boolean
It is an optional field. If it is set to true, then it automatically creates an index on _id fields. Its default value is false.
3.
size
number
It is an optional field. It is used to specify the maximum size in bytes for a capped collection. When capped is set to true, then we always need to specify this field.
4.
max
number
It is an optional field. It is used to specify the maximum number of documents which are allowed in the capped collection.
Note:-When a document is inserted in the MongoDB, it first checks the size field of the capped collection, after which it checks max field.
Example of Collection without options
We can use the following commands to create a collection without options.
> use COLLECTION_DB switchedto dbCOLLECTION_DB > db.createCollection("myFirstCollectionWithoutOptions") { "ok" : 1 } > showcollections myFirstCollectionWithoutOptions > Explanation of script Firstly we created a new database schema i.e. COLLECTION_DB with the help of ‘use’ command. Next, we used the ‘db.createCollection (“myFirstCollectionWithoutOptions”)’ method to create a named collection i.e. myFirstCollectionWithoutOptions. Here, we have used only the ‘name’ parameter. Options parameter is not used here as it is optional. Lastly, we asked MongoDB to display the list of available collections by using the ‘show’ command as shown above.
Example of Collection with options
We can use the following commands to create a collection with options.
> use COLLECTION_DB switchedto dbCOLLECTION_DB > db.createCollection("myFirstCollectionWithOptions", {capped : true, autoIndexID : true, size : 12285600, max : 30000 } ) { "ok" : 1 } > showcollections myFirstCollectionWithOptions myFirstCollectionWithoutOptions > Explanation of script Firstly, we created a new database schema i.e. COLLECTION_DB with the help of ‘use’ command. Next, we used the ‘db.createCollection (“myFirstCollectionWithOptions”, {capped: true, autoIndexID: true, size: 12285600, max: 30000})’ method to create a named collection i.e. myFirstCollectionWithOptions. Here, we have used the ‘name’ parameter as well as the ‘Options’ parameter. The ‘Options’ parameter has fields such as ‘capped’ set to true, autoIndexID set to true and since we have set ‘capped’ to true therefore, we have specified the size as 12285600 and the max filed value as 30000. Lastly, we asked MongoDB to display the list of available collections by using the ‘show’ command as shown above.
Automatic creation of collection in MongoDB
In Mongodb, the collection can be created automatically without using the ‘createCollection (name, options)’ method. We can create a collection automatically, when we insert any document in the Mongo DB as shown below.
> use COLLECTION_DB switchedto dbCOLLECTION_DB > db.COLLECTION_AUTO.insert({ ... heading: 'Life of an awesome girl', ... publish_date: '2016-11-27', ... description: 'An awesome girl...', ... permalink: 'http://awesomeblog.com/2016/12/life-of-an-awesome-girl', ... categories: ['Make', 'Travel', 'Fiction', 'girl'],likes: 56, ... comments: [{ ... name: 'Mohit', ... email:'mohit@gmail.com', ... phone:'123456789', ... message:'awesome blog', ... creationDate:'2016-11-27', ... like_flag:true},{ ... name: 'Manu', ... email:'mohit@gmail.com', ... phone:'123456789', ... message:'I like this blog very much', ... creationDate:'2016-11-27', ... like_flag:true} ...] ... }) WriteResult({ "nInserted" : 1 }) > showcollections COLLECTION_AUTO myFirstCollectionWithOptions myFirstCollectionWithoutOptions > Explanation of script Firstly, we created a new database schema i.e. COLLECTION_DB with the help of ‘use’ command. Next, we used an automatic approach to create a collection by using the insert method along with the collection name as COLLECTION_AUTO as shown above. Inside the insert method, we have given t