Hello Programmers! In previous chapter we have seen How to create/remove database using MongoDB shell, in this chapter will see How to create/remove collection using MongoDB Shell? But before we go ahead we should know that what the MongoDB collections are?

MongoDB Tutorials - How to create-remove collections using MongoDB shell
MongoDB Tutorial : How to create/remove collection using MongoDB Shell? - Progrramers

What is MongoDB Collections?

A group to MongoDB documents can be referred as a collection. A collection is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Data in MongoDB can have different schema documents in the same collection.

> db.teamindia.find().toArray()
[
{
"_id" : ObjectId("5d5e2e6cd1c0b7eafe211f13"),
"name" : "Sikhar Dhawan",
"role" : [
"Batsman"
],
"batting" : "Left-Hand Opener"
},
{
"_id" : ObjectId("5d5e2e6cd1c0b7eafe211f14"),
"name" : "Rohit Sharma",
"role" : [
"Batsman",
"Vice Captain"
],
"batting" : "Right-Hand Opener"
},
{
"_id" : ObjectId("5d5e2e6cd1c0b7eafe211f15"),
"name" : "Kedar Jadav",
"role" : [
"Allrounder",
"Batsman",
"Bowler"
],
"batting" : "Right-Hand Middle-Order",
"bowling" : "Right-Arm-Slinger-Off-Break"
},
{
"_id" : ObjectId("5d5e2e6cd1c0b7eafe211f16"),
"name" : "Rabindra Jadeja",
"role" : [
"Allrounder",
"Batsman",
"Bowler"
],
"batting" : "Left-Hand Lower-Middle-Order",
"bowling" : "Left-Arm-Off-Break"
},
{
"_id" : ObjectId("5d5e2e6cd1c0b7eafe211f17"),
"name" : "Jasprit Bumrah",
"role" : [
"Batsman",
"Bowler"
],
"batting" : "Right-Hand Talender",
"bowling" : "Right-Arm-Fast-Medium"
}
]
>

db.createCollection() Method

Below is the basic syntax of Create Collection Command:

db.createCollection(<name>, <options>);
db.createCollection() method take two parameters:
  • Name : Name of the collection to be created
  • Option : Specify options about memory size and indexing (Optional)
In the command, name is name of collection to be created. Options is a document and is used to specify configuration of collection.

> use cricket
switched to db cricket
> db.createCollection("teamindia");
{ "ok" : 1 }
> □

Above example explains how you can create a basic collection without use of options.

Parameter Type Description
Name String Name of the collection to be created
Options Document (Optional) Specify options about memory size and indexing

Options parameter is optional, so you need to specify only the name of the collection like above example. Following is the list of options you can use :

Field Type Description
capped Boolean (Optional) If true, enables a capped collection. Capped collection is a fixed size collection that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you need to specify size parameter also.
autoIndexId Boolean (Optional) If true, automatically create index on _id field.s Default value is false.
size number (Optional) Specifies a maximum size in bytes for a capped collection. If capped is true, then you need to specify this field also.
max number (Optional) Specifies the maximum number of documents allowed in the capped collection.

Now let’s try to create a collection using few parameters like mentioned in above table.

> db.createCollection("players", { capped : true, autoIndexId : true, size :
... 10485760, max : 20000 } )
{
"note" : "the autoIndexId option is deprecated and will be removed in a
future release",
"ok" : 1
}
>□

Example Explanation

Above is an example of creating a collection using ‘capped’, ‘autoIndexId’, ‘size’ and ‘max’ parameters. ‘capped’ parameter enables capped collections that has a fixed size which is specified in ‘size’ parameter - 10485760 bytes (10MB). After the reaching at its size limit, it will overwrite the previous entries. This collection can store maximum 20000 documents that specified in max parameter. autoIndexId parameter creates on _id field by default.

Note: you have to be careful while using autoIndex parameter because the autoIndexId option is deprecated and will be removed in a future release

Collections can be created automatically

MongoDB creates a new collection while you are inserting first document using a collection name and after that you can also insert documents in that collection. So you do not need to create the collections, collections can be created automatically.

> db.language.insert({

... name:"JavaScript",
... type:"Client Side",
... tutorial: "Progrramer JavaScript Tutorials"
... });
WriteResult({ "nInserted" : 1 })
> show collections
language

players
sample
system.indexes
teamindia
> db.language.find().toArray()

[
{
"_id" : ObjectId("5d62d624ed9a7864410433ef"),
"name" : "JavaScript",
"type" : "Client Side",
"tutorial" : "Progrramer JavaScript Tutorials"
}
]
>□


In above example collection named ‘language’ is created when first document is inserted.

Show Collections

In MongoDB, to view the list of collections inside a database show collection command is use.
Below example shows first switch to database using use command and then get list of collections by using show collections command.

> show collections
players
sample
system.indexes
teamindia
>

Remove Collections

To remove or delete the collection in MongoDB drop() command is used. Below is the basic syntax of drop() collection.

db.collectionname.drop();

You can check the collection in collections list and drop the collection. In below example a collection named language is dropping.

> show collections
language
players
sample
system.indexes
teamindia


> db.language.drop()
true


> show collections
players
sample
system.indexes
teamindia
>
progrramers-logo

progrramers

Hello friends! Progrramers is a tutorial site of w3 programming. If you like this tutorial site please encourages us by sharing this site links with your friends and nears & dears who want to learn web development and give us like on our Facebook page. If have any question please type in to comment box or send us message on social media sites via below given social links. Thank you, have a nice learning.

Post A Comment:

0 comments: