Enhanced CRUD Operations with Mongoose in Node.js

murasolimathavi 23 views 13 slides Jun 23, 2024
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

To implement CRUD operations (Create, Read, Update, Delete) using Mongoose with Node.js


Slide Content

CRUD OPERATION USING MOONGOOSE A Guide to Managing MongoDB with Mongoose Soniya [22EC161] Sree Vishnu Varthini [22EC162] Sree Nithi [22EC163] Sri Krishna [22EC164]

Agenda Introduction Setting Up Mongoose Defining a Schema Create Operation Read Operation Update Operation Delete Operation Mongoose Middleware Conclusion

Introduction to Mongoose

Introduction npm install mongoose Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. Provides a straight-forward, schema-based solution to model your application data. Why use Mongoose? Schema validation Middleware support Query building

Setting Up Mongoose Installation : npm install mongoose Connecting to MongoDB: npm install mongoose

Defining a Schema Example Schema: Creating a Model: Creating a Model:

READ Operation Description: Retrieving documents from the collection. Example: Finding by ID: Retrieving documents from the collection.

Delete Operation Description: Removing documents from the collection. Example: Retrieving documents from the collection.

Update Operation Description: Modifying existing documents in the collection. Example: Retrieving documents from the collection.

Mongoose Middleware: Pre Hooks Pre hooks are functions that run before a specified document operation occurs. They are often used for validation, modification, or setting defaults on the document before it gets saved to the database. The next function must be called to move to the next middleware or to complete the operation. Retrieving documents from the collection. Purpose: To ensure all email addresses are stored in lowercase, facilitating case-insensitive email lookups. How it works : Before saving a user document, the email field is converted to lowercase. The next() function is called to proceed with the save operation.

Mongoose Middleware: Post Hooks Post hooks run after a specified document operation has completed. They can be used for logging, sending notifications, or performing cleanup tasks. Unlike pre hooks, post hooks have access to the document and the result of the operation. Retrieving documents from the collection. Purpose : To log a message every time a new user is created and saved to the database. How it works : After a user document is successfully saved, the hook logs a message along with the saved document (doc). The next() function is called to complete the process.

Conclusion Mongoose simplifies interactions with MongoDB through schemas and models. CRUD operations (Create, Read, Update, Delete) are essential for database management. Middleware provides additional control over database operations. Retrieving documents from the collection.

Thank you Soniya [22EC161] Sree Vishnu Varthini [22EC162] Sree Nithi [22EC163] Sri Krishna[