MongoDB CRUD
MongoDB CRUD refers to the Create/Insert, Read, Update, and Delete operations.
Table of Contents
In today’s data-driven world, efficient data management is crucial for businesses to thrive. When it comes to handling vast amounts of information, MongoDB has emerged as a powerful player in the field of NoSQL databases. MongoDB’s flexibility, scalability, and document-oriented model make it an ideal choice for modern application development.
In this blog post, we will take you on a journey through the fundamental operations of MongoDB CRUD (Create, Read, Update, Delete). Strap in, as we uncover the unique capabilities of MongoDB and how it can revolutionize your data management strategies.
MongoDB CRUD Operations:
All the MongoDB CRUD operations that we need for creating, reading, updating & deleting are available on the collection.
Create Operations:
The create or insert operations are used to insert or add new documents in the collection. If a collection does not exist, then it will create a new collection in the database.
The “C” in MongoDB CRUD stands for “Create,” and it marks the beginning of your MongoDB journey. MongoDB excels at handling unstructured or semi-structured data, thanks to its document-oriented architecture.
There are two methods that we need to create documents in collection.
- insertOne
- insertMany
Why not add data to our classseven
collection in school
db.
# insert one classseven student > db.classseven.insertOne({ name: "def", age: 16, rank: 40 }) # insert more than one classseven students at once > db.classseven.insertMany([ { name: "Abc", age: 16, rank: 44 }, { name: "jkl", age: 16, rank: 40 }, { name: "cde", age: 16, rank: 45 }, { name: "def", age: 16, rank: 40 } ])
Read Operations:
The Read operations are used to retrieve documents from the collection. so, read operations are used to query a collection for a document.
The “R” in MongoDB CRUD represents “Read” operations, which are essential for retrieving data from your MongoDB collections. MongoDB offers a rich querying system that enables you to search, filter, and sort your data with ease.
For reading data from our collection, we can just need two methods. These methods have several operators and options that can help us filter our data according to our needs.
- find: returns multiple documents
- findOne: returns the first document only
# find one student > db.classseven.findOne({ name: "def" }) # find all student with specific rank on classseven > db.classseven.find({ rank: 40 }) # find classseven students with rank greater than 40 > db.classseven.find({ rank: { $gt: 40 } })
Update Operations:
The update operations are used to update or modify the existing document in the collection.
Updating data is a critical aspect of any data management system. MongoDB simplifies the update process through its flexible data model. We’ll walk you through various update operations, including modifying specific fields, adding new fields, or performing array manipulations.
Again, we have two methods for updating documents
- updateOne
- updateMany
Both take a filter, just like find, to find the document that we need to update and an object that defines what we want to update. We use $set
operator to update fields in a document.
# update one classseven student > db.classseven.updateOne( { name: "def" }, { $set: { rank: 45 }} ) # update multiple classseven student and add 18 to age where rank is 40 > db.classseven.updateMany( { rank: 40 }, { $set: { age: 18 }} )
Delete Operations:
Our last operation is Delete. So, the delete operation is used to delete or remove the documents from a collection.
The “D” in CRUD signifies “Delete,” a necessary operation when it comes to maintaining data integrity and cleanliness. MongoDB offers multiple ways to delete documents or collections, whether it’s a single document, a range of documents, or an entire collection.
- deleteOne
- deleteMany
Both are similar to update and find methods. They take a filter to identify the document that we want to delete. Let’s see some examples:
# delete one classseven student > db.classseven.deleteOne({ name: "abc" }) # delete classseven student with age greater than or equal to 16 > db.classseven.deleteMany({ age: { $gte: 16 }}) # delete all classseven students !! > db.classseven.deleteMany({})
In this blog post, we embarked on a journey through the essential MongoDB CRUD operations – Create, Read, Update, and Delete. We explored the unique features and flexibility that MongoDB brings to the table, empowering developers and businesses alike to handle their data efficiently.
By understanding the intricacies of MongoDB CRUD operations, you are now equipped to leverage its power to build scalable, high-performance applications that can adapt to changing data needs. So, dive into the world of MongoDB and unlock the true potential of your data management strategies.
Hope this helps you to understand basic MongoDB CRUD operations 🙂