Collection Management In MongoDB

Share via:

Collection : It is a group of documents in a database.collection is like a table in a relational database.

The database contains collections,collections contains documents and the documents contain data in it.

Naming restriction for collections in MongoDB:

1. Collection name must begin with a letter or underscore.

2. Collection name can’t be empty or null.

3. Collection names must have only alphabets,numbers and underscore.

4. Collection names should not have special characters in it.

5. Collection names must be less than 120 bytes. There are two types of collections in MongoDB

1. Uncapped collections

2. Capped collections

Uncapped collections

1. Collection with no pre-defined number of documents that it can store.i.e it can store an unlimited number of documents.

2. It is a default type of collection

3. As there is no limit for the uncapped collections that will store data in huge volume in the database and this will impact on the overall performance of the database.

4. We can perform all the CRUD operations on the uncapped collections.

5. To create a new uncapped collection we use the below method with one parameter as the name of the collection.

Syntax: db.createcollection(“Collection_Name”)

Ex: create an uncapped collection with name students db.createcollection(“students”)

This will create an empty collection of name students in the database

Capped Collections

1. Collection with a pre-defined number of documents that it can store and having a fixed size will be allocated for that collection.i.e we can fix the number of collections that can be stored in the collection.

2. It is a non default type of collection.if we want to create it it takes some additional parameters compared with uncapped collection.

3. As the data size is fixed.the overall performance of the database will be increased.

4. We can’t perform the delete operation on the capped collection.It will be deleted automatically when the fixed memory gets exceeded.

Ex: if the document size in a collection is set to 10.if you want to add an 11th document to it.the first document will get overridden by the 11th document you inserted.

5. To create a new capped collection, we use the method mentioned below

Syntax:db.createcollection(“collection_name”{capped:true,size:100,max:100})

Ex: if you want to create a capped collection with name student with size 1000 bytes and number of documents 100

db.createcollection(“student”{capped:true,size:1000,max:100}) This will create a an empty capped collection with name student

➔ To work on the collection first you select the database in which the collection is present for that use the command below

Command : use database_name

➔ If you don’t know the databases present then use the following command.

Command : show dbs

➔ To know what are the collections in the present database.

Command : show collections

➔ From the above command you can get the collection names from the required databases.

➔ To get all the collections names in array format

Command: db.getCollectionNames( )

➔ To create a new collection with name students

Command: db.createcollection(“students”)

➔ To delete a collection with name students Command : db.students.drop( )

This will return true if the collection gets deleted from the database

➔ To get the information about the collections in database.

Command: db.getCollectionInfos()

This will return name ,type ,index and some other options related to the collections

➔ To get the detailed information about the collection named student

Command: db.student.stats( )

This will return all the information related to the collection like count, storagesize, totalindexes, scaleFactor, transactions, metadata, sessions.

Finding the document from a collection

➔ 1. To see the data in the collection named student Command: db.student.find(condition )

This will return the documents present in the specified collection

➔ 2. To get a single required document from the collection named student

Command : db.student.findOne(condition)

This will return a single document that matches the mentioned condition

Inserting documents into the collection

➔ 1. To insert one document to the collection named student.

Command: db.student.insertone(document )

Ex: db.students.insertone({key:value, key1:value1})

This will insert the provided document into the collection,if the given collection name is not present in the database it will create the collection and insert the given document into it.

➔ 2. To insert multiple documents to the collection named student

Command: db.student.insertMany([{document 1}, {document 2}, {document 3}, {document n}]) Ex:db.student.insertMany([{key:value,key1:value1},{key:value,key1:value1}, {key:value,key1:value1},{key:value}])

This will insert multiple documents to the collection.similar with the above collection if the specified collection is not present in the database then it will create a new collection with the name given.

Deleting the documents

➔ 1. To Delete a single document from the collection with specific conditions like named student with the name akash

Command : db.student.deleteOne({name:akash})

This will delete the single document with name akash from the collection student

➔ 2. To delete multiple documents from a collection with a specific condition

like with age 25.

Command: db.student.deleteMany({age:25})

This will delete all the documents with having age as 25

Updating the documents

➔ 1. Update the document from the collection named student with name raj update age to 27

Command: db.student.updateOne({name:”raj”},{$set:{age:27}}) This will update the age of the raj to 27 as specified.

Counting the documents

➔ 1. To know the number of documents present in the collection named student with specific condition like name as sai

Command : db.student.count({name:sai})

This will return all count i.e number of documents with the given specified condition.

 

Author    : Teja

LinkedIn : https://www.linkedin.com/in/teja-sai-nadh-reddy-tatireddy-048882201

Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates

KTExperts is always active on social media platforms.

Facebook  : https://www.facebook.com/ktexperts/
LinkedIn    : https://www.linkedin.com/company/ktexperts/
Twitter       : https://twitter.com/ktexpertsadmin
YouTube   :  https://www.youtube.com/c/ktexperts
Instagram  : https://www.instagram.com/knowledgesharingplatform

 

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Add Comment