In the previous lessons, we looked at how we might implement CRUD using MongoDB. 1
Now in this lesson, I want to show you how you might establish relationships in your database with MongoDB. 2
There's two main ways of doing this 3
and the first way is in most cases the preferred method. 4
But what you choose will depend on the architecture of your data. 5
So it depends on how your data relates to each other and how it's structured. 6
Coming back to our shop database, let's say that we wanted to have another product so we might say something 7
like db.products.insert 8
and in this case the product that we want to insert happens to be a rubber. 9
So we might say that this one has an id of 3 10
and it's got a name of Rubber. 11
It's got a price of 1.30 and a stock of 43. 12
But this Rubber has also received reviews on our website. 13
And so we can represent these reviews using an array. 14
And inside the array we might have- we might have a few embedded documents. 15
Every document is represented by a set of curly braces. 16
And if we had a document that will represent the data for a review then we could embed that into another 17
document. 18
So our reviews document might have say an authorName 19
so in this case it was somebody called Sally, and it might have a rating 20
so they said that this was 5 stars. 21
And then it might have a review 22
right? 23
Best rubber ever. 24
OK so this would be a single document and because we're embedding it inside this particular product 25
then we can associate the relationship in this way. 26
And it's very very natural because it means that if we had another review come in for this rubber product 27
then we can simply append it into that array and we simply have some different data for it. 28
And now we've established a relationship between a single product document and a single review document. 29
Now this style of embedding documents in each other is really well-suited to this kind of one to many 30
kind of relationship because one product might have many reviews or one user might have created many 31
comments. And this is a very natural way of expressing that relationship using MongoDB. 32
I'm going to copy and paste this into my Mongo shell. 33
Now the reason why I'm doing it inside Atom is just because it is much easier to format it for you to 34
see what's going on and inside the shell 35
it's much harder to hit enter without executing your statement. 36
So let's hit paste and hit enter. 37
And now I've inserted one document into my database and if I look through all of my products I now have 38
a product with id 1 and a product with id 3. As a challenge 39
I want you to practice embedding documents inside other documents. 40
I want you to recreate that pencil document that we deleted earlier on and to add in some reviews for 41
that document as well. 42
The challenge is to follow the format that we created the last document and you're going to add two 43
reviews to this pensil product which looks something like this 44
but you're going to make up two reviews for the pencil and that each going to have a author name, a rating 45
and also a review. 46
Pause the video now and give that a go. 47
OK. 48
So I recommend writing this out inside a blank document in Atom just because it makes it so much easier 49
to write multi-line code. 50
So we're going to use db.products to tap into our products collection and then we're going to say insert 51
One and the product that we're going to insert is going to have a id of 2, name of pencil, price of 80 52
cents, stock of 12 53
and finally it's going to have that review field which is going to be an array of imbedded documents. 54
Each embedded document will be enclosed in a set of curly braces 55
and we're just going to make up some reviews right? 56
So now we can check that we don't have any errors in our code and we can copy the entire thing over 57
to our Mongo shell hit paste and hit enter. 58
And now we've inserted our second document. And now you can see we've got a pen, our pencil and our rubber 59
in our products collection. 60
Now another format you might see out there in the wild is you might say have two product documents, one 61
which is the pen and the other which is the pencil and they each have a unique identifying id which 62
is simply 1 and 2. 63
And then you could create another collection say a collection of orders this time. 64
And for each document in that collection you might have a order number but you might also have a field 65
that's products ordered and this can simply be an array that references the id of the products in the 66
products collection. 67
And so at a later date you can pull up the product ordered and you can find which products they are 68
based off those IDs.