CRUD Operations

Shahariyer Ahammed
3 min readDec 29, 2021

MongoDB is a NoSQL database program that is a document-oriented database program. It has gained popularity as NoSQL databases are handy for working with large sets of data. Simple CRUD operations have been described below.

Why is CRUD so important?
CRUD is constantly used for anything related to database and database design. Software developers can’t get anything done without CRUD operations. Website development, for example, uses REST (Representational State Transfer), which is a superset of CRUD used for HTTP resources.
On the other end, CRUD is just as important for end-users. Without it, things like registering for websites, creating blogs, or bookmarks would be impossible. Most applications we use let us add or create new entries, search for existing ones, make changes to them or delete them.
CRUD offers many benefits including:
It facilitates security control by satisfying the various access requirements
It simplifies and facilitates application design making it more scalable
It has a better performance compared to ad-hoc SQL statements

MongoDB is a document-oriented database program widely classified as a NoSQL database program.
In MongoDB, the CRUD operation refers to the creating, reading, updating, and deleting documents. Here is an explanation of the operations in detail:

Create
Create (or insert) operations add new documents to a collection. There are two ways to add new documents to a collection:

  • db.collection.insertOne()
  • db.collection.insertMany() insertOne() operation allows us to create individual documents in a collection, while the insertMany() operation is used to create multiple documents in a single operation.

Example 1:
Here is an example of how we can add a single car to the cars collection using the insertOne() operation:

db.cars.insertOne(
//inserting Bugatti Veyron Mansory Vivere-2005 into cars collection
{
name: "Bugatti Veyron Mansory Vivere"
model: "2005"
}
)

Example 2:
Now we will see how we can add info of multiple cars to the cars collection with a single operation using insertMany().

db.cars.insertMany([{
//inserting three cars along with their models into cars collection
name: "Bugatti Veyron Mansory Vivere"
model: "2005"
},{
name: "Aston Martin AM-RB 001"
model: "2018"
},{
name: "Ferrari Pininfarina Sergio"
model: "2013"
}])

Read
Read operations retrieve documents from a collection. Here is the method in MongoDB to retrieve information:

  • db.collection.find()
    find() operation will return everything from a collection if you call it without any parameters. On the other hand, we can specify any filter or criteria to retrieve information from a collection using:
  • db.collection.find(query)
    Example:
    Here is an example of how we can read information about all cars from the cars collection:
    db.cars.find() // no parameters

Output:

{ "_id" : ObjectId("1"), "name" : "Bugatti Veyron Mansory Vivere", "model" : "2005" }
{ "_id" : ObjectId("2"), "name" : "Aston Martin AM-RB 001", "model" : "2018" }
{ "_id" : ObjectId("3"), "name" : "Ferrari Pininfarina Sergio", "2013" : "2005" }

Update

Update operations modify existing documents in a collection. There are three ways to update documents of a collection:

  • db.collection.updateOne()
    – Updates one field in the document where the given criteria or filter meets the condition. Updating a field will not remove the old field instead a new field will be added to the document.
  • db.collection.updateMany()
    – Updates all fields in the document where the given criteria or filter meets the condition.
  • db.collection.replaceOne()
    – Replace the entire document. It will replace the old fields and values with new ones.
    Example: if we have the following document:
{
"_id" : ObjectId("1"),
"model" : 2005
}

Using:
replaceOne({"_id" : ObjectId("1")}, { "new_model" : 2020})
will result in:

{
"_id" : ObjectId("1"),
"new_model" : 2020
}

While using:
updateOne({"_id" : ObjectId("1")}, {$set: { "new_model" : 2020}})
Will result in:

{
"_id" : ObjectId("1"),
"model" : 2005,
"new_model" : 2020
}

While using:
updateMany({"_id" : ObjectId("1")}, {$set: { "name" : "NewName"}, $set: { "new_model" : 2020}})
will result in:

{
"_id" : ObjectId("1"),
"model" : 2005,
"new_model" : 2020
"name" : "newName"
}

Delete
Delete operations delete documents from a collection. There are two methods to delete documents of a collection:

  • db.collection.deleteOne()
  • db.collection.deleteMany() deleteOne() method removes only the first document matched by the query filter document, and deleteMany() deletes multiple objects at once. Example 1: Here is an example of how we can remove only one car having the model “2013” from the cars collection: db.cars.deleteOne( //deletes one car having model "2013" { "model": "2013" } ) Example 2: Here is an example of how all cars, having the model “2013” - can be deleted from the cars collection:
db.cars.deleteMany(
//delete all cars having model "2013"
{ "model": "2013" }
)

--

--