DEV Community

Cover image for What is CRUD!
Fahim Hossain
Fahim Hossain

Posted on

What is CRUD!

CRUD is a short form of Create, Read, Update & Delete. These 4 steps are the process of creating, reading, updating and deleting methods in Database. These methods can be applied in both Relational or Non-Relational databases.

We know databases are used to save user data, login data, product data or anything that needs to be stored for the applications. To Create a database or insert data, we need to create a database using the create method. To save the data and to read the data from the database we need to perform a Read method. Also there needs to be update and delete these data when needed. The Update and Delete are for deleting and updating the data in databases.

But before starting the details, in MongoDB the datas is saved into a collection format. These collections are similar to JSON file format. Now lets move to creating and interesting data into a collection. Also to perform these operations we will use Node.Js as runtime and Express.Js as the framework to get these tasks in an easier format.

In Express, Mongo, Node the CRUD operations are handled by these keywords like POST, GET, PUT and DELETE.Where

  • Create is POST
  • Read is GET
  • Update is PUT &
  • Delete is DELETE

Let's dig into the details. We will talk about the CRUD operation in MongoDB which is a non-relational database.

Create Operations
Creating or inserting operation does the task of creating a new collection or insert or add a new data into a collection. Browsers will perform Create operation when any POST request is sent to the server. Usually POST packs the data and sends it to the database.'/test', (req, res) => {
    const result = collectionName.insertOne(singleData);
Enter fullscreen mode Exit fullscreen mode

This above part of code creates a single data insert and if there is no collection name in the mongoDB then a collection will be opened according to the given collection name in the code.

Read Operations
Reading operations means doing the GET operations. We can get a single item using findOne() method or we can get the whole data to read by using find() method. In the code we can write.

app.get('/test', (req, res) => {
    const result = collectionName.find({}) //to read the whole data in an object.
    const result = collectionName.findOne(singleData);


Enter fullscreen mode Exit fullscreen mode

The above code does the reading operations between find and findOne operations, find is just used to get all the data available in the collection whereas findOne finds the specific data available in the collection.

Update Operation
To update data in the database we need to use the PUT operation. But here is a new thing. First we need to find the data using its own unique ID which is _id or anything in the MongoDB and then we can set/edit the data in it. If we look into the code the code will be like:

app.put('/test',(req, res) => {
      const filterData = { email: };
      const updateData = { $set: { role: 'admin' } };
      const result = usersCollection.updateOne(filterData , updateData );
Enter fullscreen mode Exit fullscreen mode

The above code runs a query using filter data or the data received from the request and then it updates the data in the 2nd line of the block using $set: {update data}. And then as a response it updates the data.

Delete Operation
To delete data in the database we need to use DELETE operation. The process is similar to updating the data because when it does delete a single data the data needs to be searched in the collection. In the code,

app.put('/test/:id',(req, res) => {
      const filterData = { _id: Object(id) };
      const result = usersCollection.deleteOne(filterData);
Enter fullscreen mode Exit fullscreen mode

The above code filters and gets the specific id using MongoDBs objectID method then it deletes a single item from the whole collection.

That's it for crud operation basics in mongoDB.

Top comments (0)