MongooseCheatsheet
Learn Mongoose with this comprehensive cheat sheet! Master MongoDB interactions in Node.js apps. Essential commands, examples, and concepts provided for beginners and experienced developers. Happy coding!
Install / Use
/learn @Rj1221/MongooseCheatsheetREADME
Mongoose Cheat Sheet
Introduction to Mongoose
Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It provides a straightforward and schema-based solution to model your application data and interact with MongoDB collections. This cheat sheet covers the essential concepts and commands for using Mongoose effectively.
<img src="https://miro.medium.com/v2/resize:fit:1050/1*acfAKaDI7uv5GyFnJmiPhA.png" alt="Mongoose Image" width="300px"/>Table of Contents
- Installation
- Connecting to MongoDB
- Defining a Mongoose Schema
- Creating a Model
- CRUD Operations
- Create
- Read
- Update
- Delete
- Querying with Mongoose
- Comparison Operators
- Logical Operators
- Regular Expressions
- Sorting
- Pagination
- Counting Documents
- Middleware
- Document Middleware
- Query Middleware
- Aggregate Middleware
- Validation
- Population
- Indexes
- Embedding Documents
- References
- Transactions
- Aggregation
- Virtuals
- Plugins
- Debugging
Installation
Install Mongoose in your Node.js project using npm or yarn:
npm install mongoose
Connecting to MongoDB
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/my_database', {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log('Connected to MongoDB'))
.catch((err) => console.error('Error connecting to MongoDB:', err));
Example
// getting-started.js
const mongoose = require("mongoose");
async function main() {
await mongoose
.connect("mongodb://127.0.0.1:27017/sample")
.then(() => console.log("Connected to MongoDB..."))
.catch((err) => console.error("Could not connect to MongoDB...", err));
// use `await mongoose.connect('mongodb://user:password@127.0.0.1:27017/test');` if your database has auth enabled
}
main().catch((err) => console.log(err));
const userSchema = new mongoose.Schema({
name: String,
email: { type: String, required: true }, // Added 'required' property for email
password: String,
});
const User = mongoose.model("User", userSchema);
const adder = async () => {
const ss = new User({
name: "sai",
email: "sai1234@gmail.com",
password: "1234",
});
await ss.save();
};
adder();
Defining a Mongoose Schema
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
age: { type: Number },
profileImage: { type: Buffer }, // For storing image data in the database
});
const User = mongoose.model('User', userSchema);
Creating a Model
const express = require('express');
const multer = require('multer');
const upload = multer(); // Middleware for handling multipart/form-data (file uploads)
const app = express();
app.post('/users', upload.single('profileImage'), async (req, res) => {
const { name, email, age } = req.body;
const profileImage = req.file ? req.file.buffer : undefined; // Get the image data from the request
try {
const newUser = new User({ name, email, age, profileImage });
await newUser.save();
res.send(newUser);
} catch (error) {
res.status(500).send(error.message);
}
});
CRUD Operations
Create
const express = require('express');
const multer = require('multer');
const upload = multer(); // Middleware for handling multipart/form-data (file uploads)
const app = express();
app.post('/users', upload.single('profileImage'), async (req, res) => {
const { name, email, age } = req.body;
const profileImage = req.file ? req.file.buffer : undefined; // Get the image data from the request
try {
const newUser = new User({ name, email, age, profileImage });
await newUser.save();
res.send(newUser);
} catch (error) {
res.status(500).send(error.message);
}
});
Read
app.get('/users', async (req, res) => {
try {
const allUsers = await User.find();
res.send(allUsers);
} catch (error) {
res.status(500).send(error.message);
}
});
app.get('/users/:userId', async (req, res) => {
const { userId } = req.params;
try {
const user = await User.findById(userId);
res.send(user);
} catch (error) {
res.status(500).send(error.message);
}
});
Update
app.patch('/users/:userId', async (req, res) => {
const { userId } = req.params;
const { age } = req.body;
try {
await User.updateOne({ _id: userId }, { age });
res.send('User updated successfully.');
} catch (error) {
res.status(500).send(error.message);
}
});
Delete
app.delete('/users/:userId', async (req, res) => {
const { userId } = req.params;
try {
await User.deleteOne({ _id: userId });
res.send('User deleted successfully.');
} catch (error) {
res.status(500).send(error.message);
}
});
Querying with Mongoose
Comparison Operators
const usersWithAgeGreaterThan25 = await User.find({ age: { $gt: 25 } });
const usersWithNameEqualToJohn = await User.find({ name: 'John' });
Logical Operators
const usersWithAgeGreaterThan25AndNameEqualToJohn = await User.find({ $and: [{ age: { $gt: 25 } }, { name: 'John' }] });
const usersWithAgeGreaterThan25OrNameEqualToJohn = await User.find({ $or: [{ age: { $gt: 25 } }, { name: 'John' }] });
Regular Expressions
const usersWithEmailMatchingPattern = await User.find({ email: { $regex: /example\.com$/ } });
Sorting
const sortedUsersByNameAscending = await User.find().sort({ name: 1 });
const sortedUsersByAgeDescending = await User.find().sort({ age: -1 });
Pagination
const pageSize = 10;
const pageNumber = 2;
const usersPage = await User.find().skip((pageNumber - 1) * pageSize).limit(pageSize);
Counting Documents
const totalUsers = await User.countDocuments();
Middleware
Middleware in Mongoose can be used for various purposes, such as image processing before saving an image to the database. For simplicity, we will use middleware for logging.
userSchema.pre('save', function (next) {
console.log('About to save a user.');
next();
});
Validation
const userSchema = new mongoose.Schema({
name: { type: String, required: true },
email: { type: String, required: true, unique: true, validate: /^\S+@\S+\.\S+$/ },
age: { type: Number, min: 18, max: 100 },
});
Population
const userSchema = new mongoose.Schema({
name: String,
posts: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Post' }],
});
const postSchema = new mongoose.Schema({
title: String,
});
const User = mongoose.model('User', userSchema);
const Post = mongoose.model('Post', postSchema);
const user = await User.findOne().populate('posts');
Indexes
userSchema.index({ name: 1 }); // Single field index
userSchema.index({ age: 1, email: 1 }); // Compound index
Embedding Documents
const userSchema = new mongoose.Schema({
name: String,
address: {
street: String,
city: String,
country: String,
},
});
References
const userSchema = new mongoose.Schema({
name: String,
posts: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Post' }],
});
const postSchema = new mongoose.Schema({
title: String,
});
const User = mongoose.model('User', userSchema);
const Post = mongoose.model('Post', postSchema);
Transactions
const session = await mongoose.startSession();
session.startTransaction();
try {
await newUser.save({ session: session });
await newPost.save({ session: session });
await session.commitTransaction();
} catch (error) {
await session.abortTransaction();
} finally {
session.endSession();
}
Aggregation
const usersWithTotalPosts = await User.aggregate([
{ $lookup: { from: 'posts', localField
: 'posts', foreignField: '_id', as: 'totalPosts' } },
{ $project: { name: 1, totalPosts: { $size: '$totalPosts' } } },
]);
Virtuals
userSchema.virtual('fullName').get(function () {
return this.firstName + ' ' + this.lastName;
});
Plugins
const mongoose = require('mongoose');
const timestampPlugin = require('mongoose-timestamp');
userSchema.plugin(timestampPlugin);
Debugging
Set
the DEBUG environment variable to mongoose:* to enable Mongoose debugging logs:
DEBUG=mongoose:* node your-app.js
Conclusion
In conclusion, this Mongoose cheat sheet serves as a valuable reference guide for efficiently working with MongoDB in Node.js applications. Covering key concepts such as schema definition, model creation, CRUD operations, querying, middleware, validation, population, and more, it equips developers of all levels with the knowledge to build robust and scalable applications with ease. Happy coding with Mongoose and MongoDB!
License
This project is licensed under the MIT License.
