0% found this document useful (0 votes)
1 views166 pages

My Notes Merged

Uploaded by

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

My Notes Merged

Uploaded by

eng21cs0213
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DATA MODELING

Most Important topic in database.


❖ Data modeling is the process of defining how data is stored and what relationships exist between different
entities in our data.
❖ It aims to visually represent the relationship between different entities in data

Advantages:
❖ Easier to manage data.
❖ Less memory.
❖ Easily we can query on data.
❖ Less cost.

Types of Data Model


1. Embedded Data Model
2. Reference Data Model
Embedded Data Model

It capture relationships between data by storing related data in a single document structure.
❖ It is in single structure.
❖ It is also called denormalized model.
❖ Can store only 16MB document.
❖ It leads duplication of data.

Ex: Here all details of a person are stored in one document.


Reference Data Model:
❖ References store relationships between data by including links, called references, from one document to
another.
❖ This is the most widely used modeling.
❖ Here we need more than one document to store data either in same collection or different collection.

Ex:
A Good Data Model Can be:
❖ Make it easier to manage data
❖ Less memory and CPU
❖ Reduce cost
❖ You can fetch fast query

RELATIONSHIPS:

❖ A relationship is a connection between two documents.


❖ Relationships allow you to reference and query related documents in read and write operations.
❖ MongoDB Relationships are the representation of how the multiple documents are logically connected
to each other in MongoDB.
❖ The Embedded and Referenced methods are two ways to create such relationships
TYPES OF RELATIONSHIPS:
ONE TO ONE RELATIONSHIP USING EMBEDDED DOCUMENTS:

[Link]({
_id: "joe",
name: "Joe Bookreader",

address: {
street: "123 Fake Street",
city: "Faketon",
state: "MA",
zip: "12345"
}
})

Here, all the details are stored in single document.


ONE TO ONE RELATIONSHIP USING REFERENCE DOCUMENT:

[Link](
{
_id: ObjectId("64c9ae40b154a52f1c6c978a"),
street: "123 Fake Street",
city: "Faketon",
state: "MA",
zip: "12345"
})

To fetch data use aggregation:


[Link]( [Link]([
{ {
_id: "joe", $lookup: {
name: "Joe Bookreader", from: "address",
address: ObjectId("64c9ae40b154a52f1c6c978a") localField: "address",
// store the object id of address document here, foreignField: "_id",
} as: "addressDetails"
}
}
])
ONE-TO-MANY RELATIONSHIPS WITH EMBEDDED DOCUMENTS:
ONE-TO-MANY RELATIONSHIPS WITH REFERENCE DOCUMENT:

Insert these documents first and then


Store Id’s in above document
ONE-TO-MANY RELATIONSHIPS WITH REFERENCE DOCUMENT:

In a many-to-many relationship between entities in MongoDB, you typically use a reference model,
where documents from one collection reference documents in another collection. Let's consider an
example of a many-to-many relationship between students and courses, where each student can enroll in
multiple courses, and each course can have multiple students enrolled.

In this schema: - Each document in the


`students` collection contains an array of
`ObjectId`
references representing the courses the
student is enrolled in. - Each document in the
`courses` collection contains an array of
`ObjectId`
references representing the students enrolled
in that course.
Schema:
❖ A schema is a JSON object that defines the structure and contents of your data.
❖ It allows you to validate the structure of data.
❖ It is used to specify validation rules for a documents in a human readable format.

Note: we can’t create schema for admin, local , config database.

Schema Validation:
❖ Schema validation lets you create validation rules for your fields, such as allowed data types
and value ranges.
❖ MongoDB uses a flexible schema model, which means that documents in a collection do
not need to have the same fields or data types by default.
❖ If any collection is created and some data is present.
❖ We can set the schema later.
❖ If we want to change the previous present data then we have to follow the current schema rules.
❖ While inserting new data we can change the order of the field.
❖ We can unset that extra added field.
❖ But we can’t unset the required present field.

You might also like