Embedding in MongoDB
2K views
Oct 24, 2024
Embedding in MongoDB
View Video Transcript
0:00
In this video, we are discussing embedding in MongoDB
0:04
Sometimes it may happen that one document we are putting it as a sub-document inside the
0:10
another document. So that is the concept of embedding in MongoDB. So, in MongoDB, the embedding is a technique to store related data in a single document structure
0:22
And you can store this type of documents as a sub-documents in Mongo-Db
0:26
And you can either store them in the form of an... array that is also known as the JSON array and as the JSON object
0:35
So, one document will be embedded in another document and that is known as the embedding
0:40
in MongoDB. And these particular documents can be stored either in the form of JSON object or array
0:47
consisting of JSON objects. Now let us go for one example. That is the SQL relation and embedded relation
0:56
We are going for the comparison for that. Now consider this case study
1:00
So, there is one post. This post is having the post ID, title, slug, body, published, created and updated
1:09
So this post. Again this post, we are having multiple comments. So we are having the ID here, with this ID they are related
1:17
Then post ID, author, email, body and created. And against this post we are having the multiple post tags are there
1:26
So this is the ID is there. we are having the post ID and the tag ID
1:31
Then we are having this respective tags are there containing ID and the name
1:36
So in case of SQL relational database, we'll be making such database structure
1:42
So now we can how we can represent the same in case of embedded relation in our MongoDB Just consider this example here So we are having here post ID title slug body published created updated
1:58
Then that is another document is there which has got embedded in this parent document
2:03
and it is a comments, author, email, body, created. And now that is another document is there which has got embedded here is the tags
2:13
So this is the respective embedded relation in our MongoDB. Embedded relations example, in the given document it stores information about the user
2:24
And if we want to add address field into it, we can add another key value pair for that
2:31
One person can have multiple addresses, so we will use the embedded address at a field
2:37
So now this is one address. You can find that this is one JSON object
2:42
This is another address, there is another JSON object. You can find that we are having two JSON objects and they are discrete
2:50
But a person can have multiple addresses, that is always common. Then how to represent it in our embedding in MongoDB
2:59
So now after adding the addresses and an embedded array object, the final document will look
3:05
like this. You can easily find that by defining one array. This array is containing JSON object
3:12
So, you can find that these two documents have got embedded in the parent document is there
3:18
It is having the ID, contact, date of birth, name and address
3:22
And transitions containing array of the JSON objects. So there is one object and that is another object
3:29
So in this way we have discussed how the embedding can be done in MongoDB
3:35
Thanks for watching this video
#Programming