NoSQL is a broad term that describes various database technologies that don’t conform to the traditional relational database model. CRUD stands for create, read, update, and delete and refers to the basic functions you’ll need to perform on your data. CRUD operations are usually performed using a language like SQL, but NoSQL databases often use different query languages or APIs. This can make it challenging to switch from a traditional relational database to a NoSQL database.
There are many different NoSQL databases, but some of the most popular ones include MongoDB, Cassandra, and Couchbase. And each of these databases have different query languages.
In this blog, you will come to know about NoSQL Databases and CRUD Operations.. You will also see how to run NoSQL CRUD operations using MongoDB.
Prerequisites
- Understanding of databases
What are CRUD Operations?
Organizations have to keep track of customer data, accounts, payment information, and other records that require persistent storage. The basic operations performed on such data stored in databases are known as CRUD operations. CRUD stands for Create, Read, Update, and Delete.
A relational database has tables with columns and rows. Each row describes a single data point, such as an employee’s name, address, phone number, salary, and so on. Each column represents a category of information, such as the employee’s age, gender, marital status, and so forth. These data can be manipulated using the CRUD (Create, Read, Update, and Delete) operations in a database. These operations allow users to create records, read existing records, update existing records, and delete existing records.
The operations are as follows:
Hevo Data allows you to seamlessly connect your MongoDB database or REST API sources to any destination of your choice. Whether you’re moving data to a new database, analytics platform, or other formats, our platform ensures a smooth and efficient integration process.
Why Choose Hevo Data?
- Integrate data from 150+ sources(60+ free sources).
- Simplify data mapping and transformations using features like drag-and-drop.
- Easily migrate different data types like CSV, JSON, etc., with the auto-mapping feature.
Join 2000+ happy customers like Whatfix and Thoughtspot, who’ve streamlined their data operations. See why Hevo is the #1 choice for building modern data stacks.
Get Started with Hevo for Free
1) Create
The Create operations enables users to insert new records into the database. It is same as the functioning of the INSERT function in SQL relational database application. Only an admin can add new attributes to the table. Whereas, a user can create rows and populate them with data corresponding to each attribute.
2) Read
Similar to the search function, Read enables users to look up particular records in the table and access their values. Users can look for keywords or filter the data based on customized criteria to find records.
3) Update
The Update operation modifies existing records in the database. It is useful when an existing record in the database must be changed along with all the attribute values. You can update one or more existing records into the new record by using the updateOne() or updateMany() functions.
4) Delete
Users can delete values with the Delete operation to remove records that are no longer needed. Users can delete one or more records at a time by specifying the right functions i.e, deleteOne() or deleteMany().
What are NoSQL Databases?
NoSQL is a term used to describe a non-relational database. A NoSQL database does not use the traditional table-based relational database management system (RDBMS). It’s often used in Big Data applications because they can scale more easily and handle large amounts of data. They are often more scalable than relational databases. These databases can also offer improved performance as they are designed to be distributed; they can take advantage of parallel processing to speed up data access.
NoSQL databases can also be more flexible because they don’t have to adhere to the rigid table structure of an RDBMS. However, NoSQL databases can be more difficult to query and less reliable than relational databases. NoSQL databases are increasing in popularity as more and more organizations look to adopt them for their data storage needs. There are many benefits that NoSQL databases can offer, including scalability, improved performance, and easier management.
Getting Started with NoSQL CRUD Operations in MongoDB
Depending on your preferences and development environment, there are a few different ways to implement NoSQL CRUD operations. In this case, MongoDB, a powerful NoSQL database is used to showcase the implementation of CRUD Operations.
Before implementing the CRUD operations, you first need to connect to the MongoDB Server. For that, you need to open the Mongo Shell and connect it to a MongoDB database by using the following command.
ssh admin@your_server_ip
ssh
: Stands for Secure Shell, a protocol for securely accessing remote servers.
admin
: The username used to log into the remote server.
@
: Indicates that the username is being associated with the server address.
your_server_ip
: This should be replaced with the actual IP address or hostname of the server you want to connect to.
Then you need to connect a MongoDB user with privileges to write and read data.
After doing this, you can implement the following NoSQL CRUD Operations in MongoDB in a step-wise manner.
1) Creating Documents
The Create in the NoSQL CRUD Operations in MongoDB indicates the creation of a data document.
You can create a document in MongoDB with information about historical monuments, their name, geographical location, country, and city. You can follow the below code snippet for creating the document.
{
"name": "The Pyramids of Giza",
"city": "Giza",
"country": "Egypt",
"gps": {
"lat": 29.976480,
"lng": 31.131302
}
}
name
: The name of the landmark or location.
- Value:
"The Pyramids of Giza"
city
: The city where the landmark is located.
country
: The country where the city is located.
gps
: An object containing the geographical coordinates (latitude and longitude).
lat
: The latitude of the location.
lng
: The longitude of the location.
The code is written in BSON, a binary format of JSON. The data is represented in the form of field: value pairs.
The two important commands to create documents in MongoDB are:
You need to run the following command to insert the above-created Document into Monuments, a new Collection with the insertOne() method:
db.monuments.insertOne(
{
"name": "The Pyramids of Giza",
"city": "Giza",
"country": "Egypt",
"gps": {
"lat": 29.976480,
"lng": 31.131302
}
}
)
name
: Represents the name of the monument.
- Value:
"The Pyramids of Giza"
city
: Indicates the city where the monument is located.
country
: Specifies the country of the monument.
gps
: An embedded object that holds geographical coordinates.
lat
: Latitude of the location.
lng
: Longitude of the location.
You can use the insertmany() command when you want to insert multiple inputs. Use the following command to insert multiple values in the Document.
db.monuments.insertMany([
{"name": "The Valley of the Kings", "city": "Luxor", "country": "Egypt", "gps": { "lat": 25.746424, "lng": 32.605309 }},
{"name": "Arc de Triomphe", "city": "Paris", "country": "France", "gps": { "lat": 48.873756, "lng": 2.294946 }},
{"name": "The Great Wall of China", "city": "Huairou", "country": "China", "gps": { "lat": 40.431908, "lng": 116.570374 }},
{"name": "The Eiffel Tower", "city": "Paris", "country": "France", "gps": { "lat": 48.858093, "lng": 2.294694 }},
{"name": "The Statue of Liberty", "city": "New York", "country": "USA", "gps": { "lat": 40.689247, "lng": -74.044502 }}
])
2) Reading Documents
Now, you can query the database to retrieve the Document from the database and read its data.
There are two methods to read data in MongoDB
You can use the following command to retrieve all the documents from a database:
db.monuments.find()
You can also give an objectId as an identifier to return only a specific document:
db.monuments.find({"country": "France"}).pretty()
Integrate MongoDB to MySQL
Integrate MongoDB Atlas to PostgreSQL
Integrate PostgreSQL to Snowflake
3) Updating Documents
The Update in NoSQL CRUD Operations can be implemented in MongoDB by updating the Documents using the following two methods:
You can use it to update one or all Document(s) in the database. Use the following code to update Arc de Triomphe to Arc de Triomphe de l’Étoile, it’s full name.
db.monuments.updateOne(
{ "name": "Arc de Triomphe" },
{
$set: { "name": "Arc de Triomphe de l'Étoile" }
}
)
db.monuments.updateOne(...)
: This function is used to update a single document in the monuments
collection.
- Filter:
{ "name": "Arc de Triomphe" }
: This specifies the condition to find the document that has the name “Arc de Triomphe”.
- Update Operation:
$set: { "name": "Arc de Triomphe de l'Étoile" }
: This updates the name
field of the matched document to “Arc de Triomphe de l’Étoile”.
Similarly, updateMany() can be used to add more attributes to the Documents collection.
4) Deleting Documents
The Delete in the NoSQL CRUD Operations can be implemented by deleting records from a MongoDB collection. The method to do this are as follows:
You can begin by removing the Arc de Triomphe de l’Étoile monument Document you modified previously:
db.monuments.deleteOne(
{ "name": "Arc de Triomphe de l'Étoile" }
)
Alternatively, you can delete Documents that have a common attribute.
db.monuments.deleteMany(
{ "country": "France" }
)
This time, all the monuments in France, including The Eiffel Tower and Arc de Triomphe de l’Étoile will be removed.
You can explore more about data replication in NoSQL Databases to get a better understanding of your NoSQL Data.
Conclusion
In this article, you learned about the CRUD operations and NoSQL databases. You also had an in-depth understanding of implementing the CRUD operations in MongoDB.
Now, you can move forward and use the COUNT() function to count the number of rows in a table according to your requirements.
It will take a lot of time to import and move data into your selected warehouse by using ETL for Data Analysis with MongoDB as your data source. When you consider how much money and resources are required to engage data engineers and analysts to make sense of this data, the issue becomes even more daunting.
However, with Hevo at your fingertips, you won’t have to worry about your MongoDB Data Replication demands. It will just take a few minutes to load your data into your chosen data warehouse.
Hevo’s strong integration with 150+ Data Sources (including 40+ Free Sources) like MongoDB, you can export data from your selected data sources and load it to the destination of your choice. It also assists you with reorganizing and enhancing your data so that it is ready for analysis. Now, you can readily save your time and focus on gaining insights and doing in-depth research on your data using BI solutions.
Sign up for a 14-day free trial and simplify your data integration process. Check out the pricing details to understand which plan fulfills all your business needs.
Frequently Asked Question
1. Does NoSQL use CRUD?
Yes, NoSQL databases use CRUD (Create, Read, Update, Delete) operations, just like relational databases. Still, how these operations are performed may vary depending on the database type (e.g., document, key-value, graph).
2. What are the CRUD operations in MongoDB?
In MongoDB, the basic CRUD operations are:
–Create: insertOne()
or insertMany()
-Read: find()
or findOne()
-Update: updateOne()
, updateMany()
–Delete: deleteOne()
, deleteMany()
3. What are the basic operations of NoSQL?
The basic operations of NoSQL databases are:
–Create: Add new records or documents.
-Read: Retrieve data based on queries.
-Update: Modify existing data.
–Delete: Remove data from the database.
Osheen is a seasoned technical writer with over a decade of experience in the data industry. She specializes in writing about B2B, technology, finance, and SaaS domains. Her passion for simplifying intricate technical concepts has established her as a respected expert in the field, making her an invaluable resource for those looking to deepen their understanding of data science.