0% found this document useful (0 votes)
19 views17 pages

Session 20-21-22-Mongoose ODM

The document provides an overview of Mongoose ODM, which adds structure to MongoDB documents through schemas. It outlines the steps to install Mongoose, create schemas, and perform CRUD operations in a Node application. Additionally, it explains how to add sub-documents and run the Node application to interact with the MongoDB server.

Uploaded by

nguyenldhe176088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views17 pages

Session 20-21-22-Mongoose ODM

The document provides an overview of Mongoose ODM, which adds structure to MongoDB documents through schemas. It outlines the steps to install Mongoose, create schemas, and perform CRUD operations in a Node application. Additionally, it explains how to add sub-documents and run the Node application to interact with the MongoDB server.

Uploaded by

nguyenldhe176088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Mongoose ODM

Objectives
◆ Define and construct Mongoose schemas and models

◆ Interact with MongoDB server using Mongoose from the Node application

03/06/2025 2
What Mongooes ODM?
Mongoose ODM
◆ MongoDB stores data in the form of documents
◆ No structure imposed on the document
 Any document can be stored in any collection
 Relies on developer’s discipline to maintain the structure of the documents
◆ Mongoose ODM
 Object Data Model
 Object Document Mapping
 Object relational mapping (ORM)
◆ Adds structure to MongoDB documents through schema
03/06/2025 4
Mongoose Schema
◆ Schema:
 Structure of the data to be stored
 Defines all the fields of your document and their types
• Can do validation

◆ Schema types: String, Number, Date, Buffer, Boolean, Mixed, ObjectId,


Array
◆ Schema is used to create a Model function
◆ Schemas can be nested to enable supporting embedded or subdocuments

03/06/2025 5
Install Mongoose ODM and connect to
a MongoDB Server
Installing Mongoose
◆ Create a folder named node-mongoose and move into the folder.
◆ At the prompt, type the following to initialize a package.json file in the
node-mongoose folder:

03/06/2025 7
Installing Mongoose
◆ In this folder, install Mongoose by typing the following at the
prompt:

03/06/2025 8
Implementing a Node Application
◆ Create a sub-folder named models in the node-mongoose folder.
Move to this folder.
◆ Create a file named dishes.js and add the following code to create
a Mongoose schema

03/06/2025 9
Create Mongoose Schemas
◆ dishes.js:

03/06/2025 10
Perform Database operations (CRUD) with Mongoose methods
◆ Move to the node-mongoose folder and create a file named index.js and
add the following code:

03/06/2025 11
Run Node application
◆ Make sure that your MongoDB server is up and running. Then at the
terminal prompt type the following to start the server and see the result:

npm start

03/06/2025 12
Adding Sub-documents to a Document
Add a Sub-document
◆ Update dishes.js in the models folder as follows:

03/06/2025 14
Add a Sub-document
◆ Update index.js in the models folder as follows:

03/06/2025 15
Run Node application
◆ Run the server and observe the result.

03/06/2025 16
Summary
◆ Understand Mongoose ODM
◆ Step by step to define and construct Mongoose schemas and
models
◆ Understand using Mongoose to interact with MongoDB server from
the Node application

03/06/2025 17

You might also like