Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 24, 2021 04:48 pm GMT

MongoDB CRUD Operations

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
Example: Inserts a new document into the inventory collection.

Image description

Insert Multiple Documents:

Image description

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

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

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 well 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


Original Link: https://dev.to/hossenmahmud/mongodb-crud-operations-40kg

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To