官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 枣庄市| 新兴县| 安宁市| 陇西县| 邮箱| 丰原市| 新化县| 沙洋县| 额济纳旗| 临海市| 塘沽区| 嘉兴市| 潮州市| 揭西县| 夏津县| 政和县| 慈利县| 简阳市| 尼木县| 巨鹿县| 酉阳| 特克斯县| 成安县| 鄂伦春自治旗| 精河县| 霍城县| 乐至县| 临漳县| 赤峰市| 漠河县| 洛川县| 天等县| 汝州市| 鄂伦春自治旗| 平安县| 伊春市| 项城市| 万载县| 安福县| 洞头县| 天水市|