DEV Community

Cover image for Getting Started with Mongoose: A Beginner's Guide to MongoDB and Node.js
Md Moniruzzaman
Md Moniruzzaman

Posted on

1

Getting Started with Mongoose: A Beginner's Guide to MongoDB and Node.js

Mongoose is a powerful tool that simplifies the interaction between your Node.js application and MongoDB, a popular NoSQL database. If you're new to Mongoose, this guide will help you understand the basics and get you started on building a solid foundation for your MongoDB-driven projects.

What is Mongoose?

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It provides a straightforward way to model your application data and interact with MongoDB databases. Think of it as a bridge between your application code and the database, making it easier to work with MongoDB in a structured manner.

Setting Up Mongoose:

1.Install Mongoose:
Before using Mongoose, make sure you have Node.js installed on your machine. You can install Mongoose using npm (Node Package Manager) with the following command: " npm i mongoose"

2.Connecting to MongoDB:
In your Node.js application, include Mongoose and connect to your MongoDB database using the following code:

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/database-name', { useNewUrlParser: true, useUnifiedTopology: true });
Enter fullscreen mode Exit fullscreen mode

Defining a Mongoose Schema:
A Mongoose schema defines the structure of your data. Here's a simple example for a blog post:

"

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const blogSchema = new Schema({
  title: String,
  content: String,
  author: String,
  date: { type: Date, default: Date.now }
});

const Blog = mongoose.model('Blog', blogSchema);
Enter fullscreen mode Exit fullscreen mode

"
This schema defines a basic structure for a blog post, including title, content, author, and a date.

Performing CRUD Operations:

1.Creating a Blog Post:
To create a new blog post, you can use the create method provided by Mongoose:

Blog.create({
  title: 'Getting Started with Mongoose',
  content: 'This is a beginner-friendly guide to Mongoose.',
  author: 'Your Name'
});
Enter fullscreen mode Exit fullscreen mode

2.Reading from the Database:
To retrieve blog posts, you can use the find method:

Blog.find({}, (err, blogs) => {
  if (err) throw err;
  console.log(blogs);
});
Enter fullscreen mode Exit fullscreen mode

3.Updating a Blog Post:
To update an existing blog post, you can use the updateOne method:

Blog.updateOne({ title: 'Getting Started with Mongoose' }, { content: 'Updated content.' }, (err, result) => {
  if (err) throw err;
  console.log(result);
});
Enter fullscreen mode Exit fullscreen mode

4.Deleting a Blog Post:
To delete a blog post, you can use the deleteOne method:

Blog.deleteOne({ title: 'Getting Started with Mongoose' }, (err) => {
  if (err) throw err;
  console.log('Blog post deleted.');
});
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay