Welcome to our tutorial on Mongoose Schemas and Models! In this lesson, we'll learn how to create and manage powerful database structures using Mongoose, an Object Data Modeling (ODM) library for Node.js that makes working with MongoDB more efficient.
š” Pro Tip: Think of a Mongoose schema as a blueprint for creating documents in MongoDB and a model as an interface to this schema.
A Mongoose schema is a set of rules that defines the structure of a MongoDB collection. It tells Mongoose how to shape the data that will be stored in a specific collection.
A Mongoose model is a constructor function that we use to create instances of our data (documents) based on the schema. It also provides useful methods for interacting with these documents.
Let's create a simple Mongoose schema for a User collection:
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
name: { type: String, required: true },
email: { type: String, required: true, unique: true },
age: Number,
isAdmin: { type: Boolean, default: false }
});š Note: We imported the Mongoose library, created a new schema with the required fields, and set default values for some fields.
Now that we have our schema, we can create a model:
const User = mongoose.model('User', userSchema);With our model in place, we can now create and save a user:
const newUser = new User({
name: 'John Doe',
email: 'john.doe@example.com',
age: 30,
isAdmin: false
});
newUser.save((error) => {
if (error) {
console.error(error);
} else {
console.log('User saved successfully');
}
});š” Pro Tip: We created a new instance of the User model, set its properties, and saved it to the database.
We can retrieve users from the database using various methods provided by the Mongoose model:
User.find({}, (error, users) => {
if (error) {
console.error(error);
} else {
console.log(users);
}
});š” Pro Tip: The find method fetches all the users from the database.
Mongoose also allows us to query our data to find specific users:
User.findOne({ email: 'john.doe@example.com' }, (error, user) => {
if (error) {
console.error(error);
} else {
console.log(user);
}
});š” Pro Tip: The findOne method retrieves a single user document that matches the given criteria.
We can update a user's information using the update method:
User.findOneAndUpdate({ email: 'john.doe@example.com' }, { age: 31 }, { new: true }, (error, updatedUser) => {
if (error) {
console.error(error);
} else {
console.log(updatedUser);
}
});š” Pro Tip: The update method updates the user's age and returns the updated document with the { new: true } option.
We can delete a user using the deleteOne method:
User.deleteOne({ email: 'john.doe@example.com' }, (error, result) => {
if (error) {
console.error(error);
} else {
console.log(result);
}
});š” Pro Tip: The deleteOne method removes the user document that matches the given criteria.
What is the purpose of a Mongoose schema?
What is the difference between a Mongoose schema and a Mongoose model?
That's it for today! Now you have a good understanding of Mongoose schemas and models. In the next lesson, we'll dive deeper into Mongoose and learn more advanced techniques for working with data. Happy coding! šÆ