
Hello readers,
In this article, I have demonstrated the Crud operations on MongoDB and performed the operations in both Synchronous and Asynchronous ways.
Basic knowledge of Node.js is appreciated.
NoSQL database used : Mongodb
Table of Contents:
- Introduction
- Setting up MongoDB (NoSQL)
- Synchronous Vs Asynchronous Execution
- Creating a Schema, Model, Object
- CREATE (Synchronous)
- CREATE (Asynchronous)
- READ (Synchronous)
- READ (Asynchronous)
- UPDATE (Synchronous)
- UPDATE (Asynchronous)
- DELETE (Synchronous)
- UPDATE (Asynchronous)
- Resources & References
1. Introduction
CRUD is an acronym for CREATE, READ, UPDATE & DELETE, which are rudimentary operations in the database.
In this post, I have used MongoDB as my primary database, which is a NoSQL database, unlike the traditional row/column model, it saves the data aka document in a JSON format, and performed the CRUD operations using the Node.js language.
Fundamentally Node.js works asynchronous or non-blocking by default so that it doesn’t wait in the cases where CPU is used intensively by a code.
Thus designing CRUD operations as synchronous or asynchronous based on our case is a challenging process, particularly when we used it in a real-time server operation, in the following sections I have performed those operations in both sync/async ways.
In this post, I have created and used a movie database throughout, which contains the details related to a specific movie, so that you can manipulate it based on your use case.
2. Setting up MongoDB (NoSQL)
MongoDB is a document database, which means it stores data in JSON-like documents. We believe this is the most natural way to think about data and is much more expressive and powerful than the traditional row/column model.
We need two components to set up the MongoDB browser.
- MongoDB Community Server – Download here
- MongoDB Compass (GUI Viewer) – Download here


After installing both the applications, we need to add the MongoDB server path to our environment variables in your PC.
So navigate yourself in the C directory of your PC and find the location of the mongod.exe file and copy the address path.
In my case, I found the address to be: C:Program FilesMongoDBServer4.4bin
Add the above location to environment variables like the below.

Now we are ready to create documents in our MongoDB database.
Also, we can view the database in MongoDB Compass, after creating the first document.

3. Synchronous Vs Asynchronous Execution
In NodeJS we can perform database CRUD operations in both synchronous (blocking) and asynchronous (non-blocking) ways.
Sometimes while doing CRUD operations, we might need the variable to send via API or use it in the subsequent codes, etc., In that case, we use synchronous programming, where a line in the code waits for the previous line to finish before its execution.
This is a time-consuming process, so alternatively we can do the operations asynchronously. This is purely based on the use case that we are working on.
Here I will demonstrate both ways to perform the same CRUD operations.
4. Creating a Schema, Model, Object
Schema is like a blueprint which specifies, what to store on the database and in which format.
For taking this post in a more comfortable way, as I said previously I will be creating a movie database that contains the movie properties such as,
- Movie name – (String),
- Director – (String)
- IMDB rating – (Number)
- Cast – (Array)
- Release date – (Datetime)
- Genre – (String)
- Sequel – (Boolean)
The primary reason for storing the above elements is to incorporate all kind of data structures like String, Boolean, Number, Array, DateTime, etc.,
So in the code snippet above, I have created the movieSchema which contains the blueprint of the document.
And later I create a constant movieClass which is a model, created based on the schema.
Using the model, we can create an instance aka object which is used for a new entry in the document. It is very similar to the OOPS concept of class and objects.
And as you can see, I have created a new object called ‘tenet’ to resemble the film name, populated the fields, and later saved it. Now if we check the GUI database viewer – MongoDB Compass, we must see the new document created.

Now let’s proceed further for CRUD operations in both synchronous and asynchronous ways.
5. CREATE (Synchronous)
We have already created a new document named after ‘tenet’ previously. So now we create a function that can be used to create many movie listings hereafter.
After the function insertMovie is declared, we can do a function call with its input parameters, which needs to be updated in the database. Once they’re inserted, we will get the document that is inserted.
Here we have inserted 3 movies.
6. CREATE (Asynchronous)
Similarly, we can insert new documents in the Mongo DB, without waiting or otherwise no code blocking. Also, I have added another 3 movie sets in no code blocking way.
7. READ (Synchronous)
Once we have created the documents, now we can filter them based on custom conditions and extract specific documents from MongoDB.
In the interests of brevity, I have created some functions using
- Fetch data by using a unique identifier
- Fetch data by using comparison operators like ( >, ≥, <, ≤, in, not in)
- Fetch data by using logical operators like (and, or)
- Fetch data by using the regex condition.
-
Fetch data and return the total count of documents.
8. READ (Asynchronous)
READ OPERATION IS STRICTLY SYNCHRONOUS
9. UPDATE (Synchronous)
There are always occurrences where we need to update the document in the future.
So, in the below functions, I have added (append) new records to an array and also replaced the Boolean and Number with new values.
We can use $push and $set array update operators reserved in MongoDB and you can try out all other operators.
You can also manipulate and create new functions based on the one’s created below, to adhere use case.
10. UPDATE (Asynchronous)
Sometimes we don’t need to wait for DB to update, so we can initiate the function and move on to the next line of code and the update can happen in due course in time.
11. Delete (Synchronous)
Typically we can also delete the record by specifying a unique id based on the documents.
12. Delete (Asynchronous)
13. Resources & References
I hope I was able to shed some light on CRUD operations on MongoDB.
Check out the GitHub repo that contains the entire code implementation and feel free to break it.
GitHub – https://github.com/bala-codes/Database-CRUD-Operations
MongoDB – https://www.mongodb.com/
Until then, see you next time.
Article By:
BALAKRISHNAKUMAR V