Web Programming TutorialsLearn the Concept of Collections in MongoDB

Learn the Concept of Collections in MongoDB

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
switched to db COLLECTION_DB
> db.createCollection("myFirstCollectionWithoutOptions")
{ "ok" : 1 }
> show collections
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.

available-collections

Example of Collection with options
We can use the following commands to create a collection with options.

> use COLLECTION_DB
switched to db COLLECTION_DB
> db.createCollection("myFirstCollectionWithOptions", {capped : true, autoIndexID : true, size : 12285600, max : 30000 } )
{ "ok" : 1 }
> show collections
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.

show-command

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
switched to db COLLECTION_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:'[email protected]',
... phone:'123456789',
... message:'awesome blog',
... creationDate:'2016-11-27',
... like_flag:true},{
... name: 'Manu',
... email:'[email protected]',
... phone:'123456789',
... message:'I like this blog very much',
... creationDate:'2016-11-27',
... like_flag:true}
...                    ]
... })
WriteResult({ "nInserted" : 1 })
> show collections
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 the script of data model for a blogging website which we had created in the last data modeling tutorial. Therefore, we can create such data model record as document and insert it into the MongoDB by using insert method with a collection name to create a collection (here COLLECTION_AUTO).
  • Lastly, we asked MongoDB to display the list of available collections by using the ‘show’ command as shown above. It will display all the three collections i.e. COLLECTION_AUTO, myFirstCollectionWithOptions, myFirstCollectionWithoutOptions, which we have created in this chapter.

three-collections

Dropping a collection
We can use the drop () Method to a collection from the database. It has the following syntax.

> db.COLLECTION_NAME.drop()

Note:- The drop () method will return true only if the assigned collection has been dropped successfully from the MongoDB, otherwise it will return false.
Example

> use COLLECTION_DB
switched to db COLLECTION_DB
> show collections
COLLECTION_AUTO
myFirstCollectionWithOptions
myFirstCollectionWithoutOptions
> db.myFirstCollectionWithOptions.drop()
true
> show collections
COLLECTION_AUTO
myFirstCollectionWithoutOptions
>

Explanation of script

  • Firstly, we created a new database schema i.e. COLLECTION_DB with the help of ‘use’ command.
  • Next, we asked MongoDB to display the list of available collections by using the ‘show’ command as shown above. It will display all the three collections i.e. COLLECTION_AUTO, myFirstCollectionWithOptions, myFirstCollectionWithoutOptions which we have created in this chapter.
  • Next, we are using the ‘drop’ method to drop the collection ‘myFirstCollectionWithOptions’ from the database as shown below.
  • Lastly, we asked MongoDB to display the list of available collections by using the ‘show’ command as shown above. This time, it will display the two collections i.e. COLLECTION_AUTO, and myFirstCollectionWithoutOptions only as the collection ‘myFirstCollectionWithOptions’ has been dropped.

two-collections

Conclusion:
In this chapter, we discussed how to create the collections in MongoDB with various options i.e. with options parameter, without options parameter and automatic creation of collection without using ‘createCollection (name. options) ‘method along with the practical examples. Later, we learnt to use the drop () method to drop a selective collection from the current database.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -