0% found this document useful (0 votes)
24 views8 pages

MongoDB CRUD Operations

Uploaded by

Manisha Yuvaraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views8 pages

MongoDB CRUD Operations

Uploaded by

Manisha Yuvaraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

MongoDB CRUD operations

MongoDB provides a set of some basic but most essential operations that
will help you to easily interact with the MongoDB server and these operations

are known as CRUD operations.


Create Operations –
The create or insert operations are used to insert or add new documents in
the collection. If a collection does not exist, then it will create a new
collection in the database. You can perform, create operations using the
following methods provided by the MongoDB:

Method Description

db.collection.insertOne() It is used to insert a single document in the collection.

It is used to insert multiple documents in the


db.collection.insertMany() collection.

db.createCollection() It is used to create an empty collection.

Example 1: In this example, we are inserting details of a single student in


the form of document in the student collection using db.collection.insertOne()
method.
Example 2: In this example, we are inserting details of the multiple students
in the form of documents in the student collection using
db.collection.insertMany()
method.
Read Operations –
The Read operations are used to retrieve documents from the collection, or
in other words, read operations are used to query a collection for a
document. You can perform read operation using the following method
provided by the MongoDB:

Method Description

db.collection.find() It is used to retrieve documents from the collection.

Example : In this example, we are retrieving the details of students from the
student collection using db.collection.find()
method.

Update Operations –
The update operations are used to update or modify the existing document in
the collection. You can perform update operations using the following
methods provided by the MongoDB:

Method Description
It is used to update a single document in the collection
db.collection.updateOne() that satisfy the given criteria.

It is used to update multiple documents in the collection


db.collection.updateMany() that satisfy the given criteria.

It is used to replace single document in the collection


db.collection.replaceOne() that satisfy the given criteria.

Example 1: In this example, we are updating the age of Sumit in the student
collection using db.collection.updateOne()
method.

Example 2: In this example, we are updating the year of course in all the
documents in the student collection using db.collection.updateMany()
method.

Delete Operations –
The delete operation are used to delete or remove the documents from a
collection. You can perform delete operations using the following methods
provided by the MongoDB:

Method Description

It is used to delete a single document from the collection


db.collection.deleteOne() that satisfy the given criteria.

db.collection.deleteMany() It is used to delete multiple documents from the


collection that satisfy the given criteria.

Example 1: In this example, we are deleting a document from the student


collection using db.collection.deleteOne()
method.
Example 2: In this example, we are deleting all the documents from the
student collection using db.collection.deleteMany()
method.

You might also like