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
•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:
Defining a Schema
Example Schema: Creating a Model:
Creating a Model:
READ Operation
Description:
Retrieving documents from the collection.
Example:
Finding by ID:
Delete Operation
Description:
Removing documents from the collection.
Example:
Update Operation
Description:
Modifying existing documents in the collection.
Example:
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.
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.
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.
Thank you
Soniya [22EC161]
Sree Vishnu Varthini [22EC162]
Sree Nithi [22EC163]
Sri Krishna[

Enhanced CRUD Operations with Mongoose in Node.js

  • 1.
    CRUD OPERATION USINGMOONGOOSE A Guide to Managing MongoDB with Mongoose Soniya [22EC161] Sree Vishnu Varthini [22EC162] Sree Nithi [22EC163] Sri Krishna [22EC164]
  • 2.
    Agenda  Introduction  SettingUp Mongoose  Defining a Schema  Create Operation  Read Operation  Update Operation  Delete Operation  Mongoose Middleware  Conclusion
  • 3.
  • 4.
    Introduction •Mongoose is anObject 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
  • 5.
    Setting Up Mongoose Installation: npminstall mongoose Connecting to MongoDB:
  • 6.
    Defining a Schema ExampleSchema: Creating a Model: Creating a Model:
  • 7.
    READ Operation Description: Retrieving documentsfrom the collection. Example: Finding by ID:
  • 8.
  • 9.
    Update Operation Description: Modifying existingdocuments in the collection. Example:
  • 10.
    Mongoose Middleware: PreHooks 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. 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.
  • 11.
    Mongoose Middleware: PostHooks 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. 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.
  • 12.
    Conclusion •Mongoose simplifies interactionswith MongoDB through schemas and models. •CRUD operations (Create, Read, Update, Delete) are essential for database management. •Middleware provides additional control over database operations.
  • 13.
    Thank you Soniya [22EC161] SreeVishnu Varthini [22EC162] Sree Nithi [22EC163] Sri Krishna[