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 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:
Insert a Single Document:
Insert Multiple Documents:
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:
We can specify query filters or criteria that identify the documents to return.
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.
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.
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:
We can specify criteria, or filters, that identify the documents to remove. These filters use the same syntax as read operations.