MongoDB CRUD Operations
Tags: javascript, mongodb
Published: 2021-12-24 16:48:06

CRUD stands for Create, Read, Update and Delete. The capacity to create, read, update and delete items in a web application is important for the development of a website and making it interactive. For example: if we are making a blog page and someone wants to post blogs, without CRUD operations it would be a real problem.

## Create
Create or insert operations add new documents to a collection. If the collection does not currently exist, insert operations will create the collection.To create data, the HTTP Post method is used, it is used to create new data for the database.
 
MongoDB provides the following methods to insert documents into a collection:

- db.collection.insertOne() 
- db.collection.insertMany() 
 
**Insert a Single Document:**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ihfxk7ik28sst89764f.png)
**Example:  Inserts a new document into the inventory collection.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pzns0vhp123disq0iddl.png)

**Insert Multiple Documents:**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9rzru9od5kl0lhoehkr0.png)


## Read
Read operations retrieve documents from a collection. Query a collection for documents. The read allows reading from the database. It would never change the data. To read a resource HTTP Get method is used. MongoDB provides the following methods to read documents from a collection:

- db.collection.find()

We can specify query filters or criteria that identify the documents to return.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3fmxbxaxzcxmlvzar5wy.png)
## Update
Update operations modify existing documents in a collection. If we store the price of stocks and after a few days it has gone up, we would want to change the previous price to the current price. It means we would update the price from the previous one. The PUT method is used for updating data from the databas. MongoDB provides the following methods to update documents of a collection.

- db.collection.updateOne() 
- db.collection.updateMany() 
- db.collection.replaceOne() 
 
In MongoDB, update operations target a single collection. All write operations in MongoDB are atomic on the level of a single document.
We can specify criteria, or filters, that identify the documents to update. These filters use the same syntax as read operations.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x4a2mp09e91eq3n7w41w.png)

## Delete
Delete operations remove documents from a collection. For example, we stored your favorite dishes on the database. We want to remove pizza from the list,then we’ll use the delete operation. For that HTTP Delete method will be used. MongoDB provides the following methods to delete documents of a collection:

- db.collection.deleteOne()
- db.collection.deleteMany() 
 
We can specify criteria, or filters, that identify the documents to remove. These filters use the same syntax as read operations.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/74tpr44nyj02xbx38hc6.png)