Creating relations between documents

We now know how to create documents in the collections of a database. However, in real life, it is usually never enough to simply have standalone documents. We will also want to establish some kind of relations between the documents.

For example, in our database, we store information about customers and products, but we also want to store information about orders, which essentially are bills of sale stating that customer X has ordered product Y.

Let's say that Jane wants to order an Pear. To achieve this, we could let our orders look like this:

{
"customer" :
{
"firstName" : "Jane",
"lastName" : "Doley"
},
"product" :
{
"name" : "Pear",
"price" : 3
}
}

However, the disadvantages of this become clear immediately. It leads to massive data bloating, since the same customer or product can occur in several orders. Hence, its data will need to be repeated in each of the orders. It also makes maintenance a nightmare. If we want to update, say, the price of a product, we need to comb through the database for every single instance where that product appears and make the change.

A much better approach, as recommended by the MongoDB developers, is to use manual references. In this approach, we only store the _id of the document that we wish to refer to rather than the full document.

Note

There are alternative methods built into MongoDB, but generally, they deal with corner cases and are not optimal for general use. Throughout this book, we will only use the method described here.

We then let the application accessing the database retrieve information about the other document(s), which are referred to as needed. Going back to our order example, this means that the final order document will instead look like this:

{
"customerId" : ObjectId("54f94003ea8d3ea069f2f652")
"productId" : ObjectId("54f8f6b8598e782be72d6295")
}

Note that we appended Id to the property names in the preceding code. This is a normal convention when dealing with references to other documents, and it is highly recommended that you follow it.

As we have come to expect from MongoDB by now, inserting this new document is no harder than the following:

db.Orders.insert({
"customerId" : ObjectId("54f94003ea8d3ea069f2f652"),
"productId" : ObjectId("54f8f6b8598e782be72d6295")
})

We can then run db.Orders.find()to assure ourselves that everything went as expected:

{
"_id" : ObjectId("54f976ccea8d3ea069f2f654"),
"customerId" : ObjectId("54f94003ea8d3ea069f2f652"),
"productId" : ObjectId("54f8f6b8598e782be72d6295")
}

It is important to note that even though our order serves no other purpose but to tie two other documents together, it still has its own unique ID.

That's it! We have now constructed a simple database for the storage of information about customers, products, and orders. Next, we will learn how to query it in order to retrieve data for it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.121.8