Mongoose CRUD Operations Simplified

By: Published: July 25, 2022

Mongoose CRUD Operations - Featured Image

Unlike traditional relational database management systems, MongoDB has a flexible schema and is highly compatible with Node.JS, a JavaScript runtime environment that makes backends efficient. Although Node.JS and MongoDB are highly compatible, there are some issues like abstraction failure.

The problem is dealt with using Mongoose, which is the middleware between MongoDB and Node.JS to ensure effective communication. With the assistance of Mongoose’s features like validators, plugins, and more, data management becomes more accessible and efficient.

In this article, you will learn how to use Mongoose CRUD operations to deal with the MongoDB database’s data efficiently.

Table of Contents

Prerequisites

Understanding of databases.

What is Node.JS?

Developed by Ryan Dahl in 2009, Node.js is an asynchronous server-side platform built upon Google Chrome’s JavaScript Engine (V8 engine). Node.js is an open-source JavaScript runtime environment mainly used for the backend development of the web applications that requires a fast, scalable, lightweight, and data-focused server.

Since it supports cross-platform, developers can run it on Microsoft Windows, Linux, and OS. The JavaScript libraries simplify the backend development of data steaming applications, single page applications (SPAs), and JSON APIs bases websites. It has a cluster module that manages load balancing for each and every active CPU cores and concurrently manages all the client-side requests efficiently.

All the APIs in the Node.JS library are asynchronous, meaning that the working of Node.JS is non-blocking and will never wait for an API to return data. Instead, Node.JS moves on to the following API and notifies the server to get the response of the previous API request made.

It uses a single-threaded model with an event looping mechanism to respond asynchronously to enhance the server by increasing its scalability and performance. With Node.JS, you can easily create dynamic page content to collect form data and perform various database operations like insert, delete, modify, etc.

What is MongoDB?

MongoDB is a NoSQL document-oriented database that stores data in a flexible and scalable manner so users can easily query or index the required data whenever needed. Developed in February 2009, the primary purpose of this open-source database was to store a large amount of data in the format of BSON (binary representation of JSON documents).

Traditional users store data in the form of tables with a fixed schema. In contrast, MongoDB provides a flexible schema to support the dynamic data of growing enterprise applications. It also offers official driver support for many popular software languages like C, C++, Ruby, Motor, Python, Java, PHP, Swift, Mongoid, Go, Node.JS, and more.

The horizontal scalability features of MongoDB, like data replication and sharding, provide its users with an intelligent operational data platform that helps them focus solely on their data to create meaningful insights. MongoDB replicates each write operation on the primary node and multiple secondary servers for data availability if the primary server crashes or fails.

A large amount of data is broken down into smaller parts called shards and is distributed equally across all these servers, and this process is called sharding. Data replication and sharding in MongoDB promote optimal load balancing that enhances better data management and performance of the database server.

Changing the structure of records is as simple as adding new fields or deleting existing ones (referred to as Documents by MongoDB). This MongoDB feature simplifies the representation of Hierarchical Relationships, Store Arrays, and other more complexes Data Structures. Many tech behemoths, including Facebook, eBay, Adobe, and Google, now use MongoDB to store massive amounts of data.

These features of the MongoDB database provided enterprises with better data availability and stability. MongoDB model indexes all the data effectively with the expansive range of indices and language-specific sorting algorithms for better execution and data retrieval.

What is Mongoose?

Mongoose js Image
Image Source

Mongoose is an Object Document Mapper (ODM) that is mostly used when the Node.JS server is integrated with the MongoDB database. It manages all the relationships between data, assists in validating schema, and translates objects in MongoDB. Mongoose provides good functionality when building and working with database schemas.

It is a JavaScript library that helps you create a model based on a particular schema. Then this model is mapped to a MongoDB document through the model’s schema specifications.

Mongoose can support eight different types of ‘SchemaTypes’: String, Number, Date, Buffer, Boolean, Mixed, ObjectId, and Array. It even has a wide range of methods to perform CRUD (create, retrieve, update and delete) operations on your data in the MongoDB database. 

Scale your data integration effortlessly with Hevo’s Fault-Tolerant No Code Data Pipeline

As the ability of businesses to collect data explodes, data teams have a crucial role to play in fueling data-driven decisions. Yet, they struggle to consolidate the scattered data in their warehouse to build a single source of truth. Broken pipelines, data quality issues, bugs and errors, and lack of control and visibility over the data flow make data integration a nightmare.

1000+ data teams rely on Hevo’s Data Pipeline Platform to integrate data from over 150+ sources in a matter of minutes. Billions of data events from sources as varied as SaaS apps, Databases, File Storage and Streaming sources can be replicated in near real-time with Hevo’s fault-tolerant architecture. What’s more – Hevo puts complete control in the hands of data teams with intuitive dashboards for pipeline monitoring, auto-schema management, and custom ingestion/loading schedules. 

All of this combined with transparent pricing and 24×7 support makes us the most loved data pipeline software on review sites.

Take our 14-day free trial to experience a better way to manage data pipelines.

Get started for Free with Hevo!

Key Features of Mongoose

Here are some features of Mongoose:

1) Middleware

Middleware, also known as pre- and post-hooks, are functions that provide control when the asynchronous methods are getting executed. Mongoose is the software between the operating system and the running application that enables data management and communication. It is established on the schema level and is beneficial for writing plugins. Mongoose has four types of middleware – document, model, aggregate, and query.

Each of these middleware types has a built-in method that helps users to interact with the MongoDB database via Mongoose. Mongoose is a middleware dealing with complex validation, eliminating dependent documents, making asynchronous defaults, and creating asynchronous functions for specific triggers. 

2) Error handling

Developers can use Mongoose to connect with the MongoDB database and efficiently deal with errors. With the Mongoose connection, there can be two classes of errors, error on initiation and error after initiation. The mistake in the initial link occurs when the connection fails, Mongoose will not automatically try to reconnect again.

However, if the error is after the connection, Mongoose will attempt to reconnect with the MongoDB database. Error handling of Mongoose is easy and can be dealt with using an inbuilt function. You can also use try-catch statements along with async or await.

3) Validation

In Mongoose, validation is defined in the SchemaType and has a wide collection of built-in validation rules. For example, the required validator checks if a certain property is not empty. Developers can put validations on all the queries they use for inserting, deleting, or updating a record in the MongoDB database.

Mongoose, lets you put maximum and minimum validators on numbers, whereas, for Strings, there are enums, match, min length, and max length validators. The validations feature helps make error handling easier, reducing development time and effort.

Mongoose CRUD Operations

MongoDB and Node.JS are commonly used in a tech stack of MERN for developing robust and interactive web applications, where M and N represent MongoDB and Node.JS, respectively. Both technologies are highly compatible, but developers faced abstraction failure when they used Node.JS and MongoDB.

Mongoose was introduced as a middleware between NodeJS and MongoDB to deal with this issue. Developers were able to take advantage of Mongoose and define schema easily. Mongoose’s features like validations, populations, middleware, and more make it easy to use MongoDB methods. 

In this article, you will learn how to perform Mongoose CRUD operations on the MongoDB database. Before you start, download MongoDB and Node.JS on your device. To use Mongoose, you must install the package via npm:

npm install mongoose --save.

Then make a connection with the MongoDB database using mongoose.connect() method. Once you have made a successful connection, let us understand how to perform Mongoose CRUD operations.

1) Create

  • Save method

To create new entries of JavaScript objects from an object function constructor, call the save() method on that object. This method will save the newly created document in the database.

#Mongoose CRUD Operations
doc.name = 'foo';
await doc.save();
  • Insert method

The two methods, insert() and insertMany() can be used to insert an array of records. The only difference is that the former returns the number of inserts and confirmation about the record inserted. In contrast, insertMany() returns the record id and inserted record.

#Mongoose CRUD Operations

User.collection.insert(
  [ <document 1> , <document 2>, ... ],
  {
     writeConcern: <document>,
     ordered: <boolean>
  }
)


User.collection.insertMany(
  [ <document 1> , <document 2>, ... ],
  {
     writeConcern: <document>,
     ordered: <boolean>
  }
)

  • Create method

Similar to the save() method, even the create() method is used to create new model documents in Mongoose. However, it is shorthand to make one or more documents in the database

#Mongoose CRUD Operations

User.create([Newuser,Newuser1,Newuser2])
  .then((data)=>{
    resolve(data);
  }).catch((err)=>{
     reject(err); 
 })

2) Retrieve 

Mongoose CRUD Operations gives you three simple ways to retrieve or read any data from the MongoDB database – find(), findOne(), and findById(). The find() method returns everything in the database that matches the query.

User.find([query],[callback])

The findOne() method returns only the first matching record.

User.findOne([query],[fieldsToReturn],[callback])

Lastly, the findById() method returns a single object in the MongoDB database based on the id you provide.

User.findById(id,[fieldsToReturn],[callback])

3) Update

An update operation first searches for the queried record in the database, retrieves it, and then modifies it according to the developer’s need. In Mongoose CRUD Operations, you can use the findByIdAndUpdate() method to update a certain record by providing its record Id. 

#Mongoose CRUD Operations

if(mongoose.Types.ObjectId.isValid(id)) {
User.findByIdAndUpdate(id,{$set:{name:user.name}},{new:true})       .then((docs)=>{
  if(docs) {
    resolve({success:true,data:docs});
  } else {
    reject({success:false,data:"no such user exist"});
  }
}).catch((err)=>{
   reject(err);
})
} else {
 reject({success:"false",data:"provide correct key"});
}

For condition-based updating, you can use the update() method; you can even update multiple files by making the multi flag true.

User.update({_id:id},{$set:{name:user.name,state:user.state}},
{multi:true,new:true})
 .then((docs)=>{
   if(docs) {
     resolve({success:true,data:docs});
   } else {
     reject({success:false,data:"no such user exist"});
   }
}).catch((err)=>{
   reject(err);
})
#Mongoose CRUD Operations

4) Delete

In Mongoose CRUD Operations, there are two methods to delete or remove a record – remove() and findOneAndRemove(). While remove() deletes all the records from MongoDB that satisfy the given condition, the findOneAndRemove() method removes only one record based on the Id you give.

User.findByIdAndRemove(id,options,callback)

Conclusion

In this article, you learned how to perform Mongoose CRUD Operations on the MongoDB database. MongoDB and Node.JS is the ideal solution for creating and managing large-scale web applications.

Along with these technologies, Mongoose is a good middleware as it improves performance and scalability through effective data management.

Using this tech stack of MongoDB, Node.JS, and Mongoose, enterprises can easily build a flexible, robust, and dynamic web application that can handle millions of users concurrently, that sounds good right, but what about the pipeline to transport data? This is where Hevo can change the game!

Hevo Data, A No-code Automated Data Pipeline provides you with a consistent and reliable solution to manage data transfer between a variety of sources and a wide variety of Desired Destinations with a few clicks.

Visit our Website to Explore Hevo

Hevo Data will automate your data transfer process, hence allowing you to focus on other aspects of your business like Analytics, Customer Management, etc. Hevo provides a wide range of sources – 150+ Data Sources (including 40+ Free Sources) – that connect with over 15+ Destinations and load them into a destination to analyze real-time data at transparent pricing and make Data Replication hassle-free.

Want to take Hevo for a ride? Sign Up for a 14-day free trial and simplify your Data Integration process. Do check out the pricing details to understand which plan fulfills all your business needs.

Share your experience of learning the Mongoose CRUD Operations in the comment section below! We would love to hear your thoughts.

mm
Freelance Technical Content Writer, Hevo Data

Vidhi possesses a deep enthusiasm for data science, with a passion for writing about data, software architecture, and integration. She loves to solve business problems through tailored content for data teams.

No-code Automated Data Pipeline for your Databases