0% found this document useful (0 votes)
11 views

CRUDin Mongodb

The document discusses the four CRUD operations in MongoDB - Create, Read, Update, and Delete. It explains each operation, the MongoDB methods to perform them, and provides examples of using each method.

Uploaded by

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

CRUDin Mongodb

The document discusses the four CRUD operations in MongoDB - Create, Read, Update, and Delete. It explains each operation, the MongoDB methods to perform them, and provides examples of using each method.

Uploaded by

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

MongoDB CRUD Operations - GeeksforGeeks about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fwww.geeksforgeeks.org%2Fmong...

geeksforgeeks.org

MongoDB CRUD Operations


GeeksforGeeks

6–7 minutes

Last Updated : 18 Apr, 2024

CRUD Operations (Create, Read, Update, and Delete) are the


basic set of operations that allow users to interact with the
MongoDB server.

As we know, to use MongoDB we need to interact with the


MongoDB server to perform certain operations like entering new
data into the application, updating data into the application,
deleting data from the application, and reading the application

data.

In this article, we will learn all 4 major operations– CREATE,


READ, UPDATE, and DELETE that form the CRUD operations
in MongoDB.

Perform CRUD Operations in MongoDB

1 of 12 5/9/2024, 3:21 PM
MongoDB CRUD Operations - GeeksforGeeks about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fwww.geeksforgeeks.org%2Fmong...

Now that we know the components of the CRUD operation, let’s


learn about each individual operation in MongoDB. We will know
what each operation does, and the methods to perform these
operations in MongoDB.

We will create, read, update and delete documents from MongoDB


server.

1. 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.

db.collection.insertMany() It is used to insert multiple


documents in the collection.

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


collection.

Create Operations Example

Let’s look at some examples of the Create operation from CRUD in


MongoDB.

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


student in the form of document in the student collection using

2 of 12 5/9/2024, 3:21 PM
MongoDB CRUD Operations - GeeksforGeeks about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fwww.geeksforgeeks.org%2Fmong...

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.

3 of 12 5/9/2024, 3:21 PM
MongoDB CRUD Operations - GeeksforGeeks about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fwww.geeksforgeeks.org%2Fmong...

2. 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.

Note: pretty() method is used to decorate the result such that it is

4 of 12 5/9/2024, 3:21 PM
MongoDB CRUD Operations - GeeksforGeeks about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fwww.geeksforgeeks.org%2Fmong...

easy to read.

Read Operations Example

Let’s look at some examples of Read operation from CRUD in


MongoDB.

Example: In this example, we are retrieving the details of


students from the student collection using db.collection.find()
method.

3. Update Operations

The update operations are used to update or modify the existing


document in the collection. You can perform update operations

5 of 12 5/9/2024, 3:21 PM
MongoDB CRUD Operations - GeeksforGeeks about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fwww.geeksforgeeks.org%2Fmong...

using the following methods provided by the MongoDB:

Method Description

db.collection.updateOne() It is used to update a single


document in the collection
that satisfy the given criteria.

db.collection.updateMany() It is used to update multiple


documents in the collection
that satisfy the given criteria.

db.collection.replaceOne() It is used to replace single


document in the collection
that satisfy the given criteria.

Update Operations Example

Let’s look at some examples of the update operation from CRUD


in MongoDB.

Example 1: In this example, we are updating the age of Sumit in


the student collection using db.collection.updateOne()
method.

6 of 12 5/9/2024, 3:21 PM
MongoDB CRUD Operations - GeeksforGeeks about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fwww.geeksforgeeks.org%2Fmong...

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.

7 of 12 5/9/2024, 3:21 PM
MongoDB CRUD Operations - GeeksforGeeks about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fwww.geeksforgeeks.org%2Fmong...

4. 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

db.collection.deleteOne() It is used to delete a single


document from the collection

8 of 12 5/9/2024, 3:21 PM
MongoDB CRUD Operations - GeeksforGeeks about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fwww.geeksforgeeks.org%2Fmong...

Method Description
that satisfy the given criteria.

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


documents from the
collection that satisfy the
given criteria.

Delete Operations Examples

Let’s look at some examples of delete operation from CRUD in


MongoDB.

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


the student collection using db.collection.deleteOne()
method.

9 of 12 5/9/2024, 3:21 PM
MongoDB CRUD Operations - GeeksforGeeks about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fwww.geeksforgeeks.org%2Fmong...

Example 2: In this example, we are deleting all the documents

10 of 12 5/9/2024, 3:21 PM
MongoDB CRUD Operations - GeeksforGeeks about:reader?url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fwww.geeksforgeeks.org%2Fmong...

from the student collection using db.collection.deleteMany()


method.

Conclusion

CRUD operations are the fundamentals of MongoDB. MongoDB


CRUD operations let user interact with the MongoDB server. They
provide operations to create, read, update and delete documents
from the collection in database.

In this article, we have explained the purpose and use of CRUD


operations in MongoDB. We have also discussed each CRUD
operation with examples to provide better understanding of the
concept.

Summer-time is here and so is the time to skill-up! More than

11 of 12 5/9/2024, 3:21 PM
5,000 learners have now completed their journey from basics of
DSA to advanced level development programs such as Full-
Stack, Backend Development, Data Science.

And why go anywhere else when our DSA to Development:


Coding Guide will help you master all this in a few months! Apply
now to our DSA to Development Program and our counsellors will
connect with you for further guidance & support.

0:00 / 0:00

You might also like