Mastering Mongoose and MongoDB with Node.js

Mongoose is a popular Object-Document Mapper (ODM) for Node.js that provides a convenient and elegant way to interact with MongoDB.

In this article, I will show you how to install and use Mongoose with Node.js to create a simple CRUD application.

Prerequisites

Before we get started, you will need to have the following installed:

  • Node.js

  • MongoDB

Installing Mongoose

To install Mongoose, open a terminal window and run the following command:

npm install mongoose

Creating a new Node.js project

Once you have installed Mongoose, you can create a new Node.js project using the following command:

mkdir my-project
cd my-project

Creating a new Mongoose schema

The next step is to create a new Mongoose schema. A schema is a blueprint for your MongoDB documents. To create a new schema, you can use the following code:

const Schema = mongoose.Schema
const UserSchema = new Schema({
  name: String,
  email: String,
});

The Schema object provides a number of methods for defining the properties of your documents. In this example, I have defined two properties: name and email.

Creating a new Mongoose model

Once you have created a schema, you can create a new Mongoose model. A model is a JavaScript object that represents a MongoDB document. To create a new model, you can use the following code:

const User = mongoose.model("User", UserSchema);

The mongoose.model() method takes two arguments: the name of the model and the schema that the model will use. In this example, I have created a model named User that uses the UserSchema that I created earlier.

Connecting to MongoDB

The next step is to connect to MongoDB. To do this, you can use the following code:

const mongoose = require("mongoose")
mongoose.connect("mongodb://localhost:27017/my-database")

The mongoose.connect() method takes a connection string as an argument. In this example, we are connecting to a MongoDB database named my-database that is running on the local machine on port 27017.

Creating a new user

Now that we are connected to MongoDB, we can create a new user. To do this, we can use the following code:

const user = new User({
  name: "John Doe",
  email: "johndoe@example.com",
})
user.save();

The save() method saves the document to the database.

Finding a user

We can find a user by their email address using the following code:

const user = await User.findOne({ email: "johndoe@example.com" });

The findOne() method returns a promise that resolves to a user document.

Updating a user

We can update a user by their email address using the following code:

const user = await User.findOneAndUpdate({ email: "johndoe@example.com" }, {
  $set: { name: "Jane Doe" },
})

The findOneAndUpdate() method returns a promise that resolves to the updated user document.

Deleting a user

We can delete a user by their email address using the following code:

await User.deleteOne({ email: "johndoe@example.com" });

The deleteOne() method returns a promise that resolves to the number of documents that were deleted.

Conclusion

In this article, we have shown you how to install and use Mongoose with Node.js to create a simple CRUD application. Mongoose is a powerful tool that can help you to easily interact with MongoDB from your Node.js applications.