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

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.

主站蜘蛛池模板: 固阳县| 恩施市| 莆田市| 中方县| 贵州省| 永嘉县| 如东县| 汾西县| 马龙县| 易门县| 长岛县| 邢台市| 时尚| 简阳市| 上杭县| 河东区| 龙州县| 巨鹿县| 靖边县| 阜阳市| 秭归县| 郓城县| 白河县| 文安县| 临颍县| 焉耆| 乐亭县| 上栗县| 聂拉木县| 怀集县| 武强县| 阜新市| 民权县| 凤凰县| 温宿县| 德格县| 华阴市| 乌恰县| 保山市| 新田县| 鹤峰县|