,

Tutorial #19: Integrating our application with MongoDB using ExpressJS and Mongoose

Posted by






Tutorial #19 : Connecting Our App with MongoDB | ExpressJS + Mongoose

Connecting Our App with MongoDB | ExpressJS + Mongoose

Today, we will learn how to connect our ExpressJS application with MongoDB using Mongoose, a MongoDB object modeling tool designed to work in an asynchronous environment.

What is MongoDB?

MongoDB is a popular open-source NoSQL database that uses a document-oriented data model. It is designed to be scalable and flexible, allowing for the storage of large volumes of data in a variety of formats.

What is Mongoose?

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js, providing a straightforward schema-based solution to model our application data. It includes built-in type casting, validation, query building, and business logic hooks, making it easy to work with our MongoDB data.

Setting up Mongoose in our ExpressJS application

To get started, we need to install Mongoose in our project using npm. In the terminal, navigate to the root folder of our project and run the following command:


$ npm install mongoose

This will install Mongoose as a dependency in our project, allowing us to use it to interact with our MongoDB database.

Connecting to MongoDB

Once Mongoose is installed, we can connect to our MongoDB database by adding the following code to our ExpressJS application:


const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/our-database', { useNewUrlParser: true, useUnifiedTopology: true });

Replace ‘our-database’ with the name of our MongoDB database. The options { useNewUrlParser: true, useUnifiedTopology: true } allow for the use of new URL string parser and the new server discovery and monitoring engine.

Creating a schema and model

With Mongoose, we can define a schema for our data and create a model that will allow us to interact with our MongoDB collection. Here’s an example of how we can create a simple user schema and model:


const userSchema = new mongoose.Schema({
name: { type: String, required: true },
email: { type: String, required: true }
});

const User = mongoose.model('User', userSchema);

Using our model to interact with MongoDB

Now that we have our User model, we can use it to perform various operations on our MongoDB collection, such as creating, reading, updating, and deleting documents.


// Create a new user
const newUser = new User({ name: 'John Doe', email: 'john@example.com' });
newUser.save();

// Find a user
User.findOne({ name: 'John Doe' }, (err, user) => {
if (err) {
console.log(err);
} else {
console.log(user);
}
});

These are just a few examples of how we can use Mongoose to interact with our MongoDB database in our ExpressJS application. With Mongoose, we can easily define and work with our data, and perform complex operations on our MongoDB collection.

By following this tutorial, we have learned how to connect our ExpressJS application with MongoDB using Mongoose, create a schema and model for our data, and use the model to interact with our MongoDB collection.

We encourage you to continue exploring Mongoose’s documentation and experimenting with its capabilities to get the most out of using MongoDB in our applications.

Happy coding!


0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Flop To Hope
7 months ago

nice video

Vijay Kumar
7 months ago

Sir, 1st video konsa hai (MERN STACK )mai kaha se shuru Karu tutorial dekhna Mera HTML , CSS , JS (fundamental) hua hai