- Learning Node.js for Mobile Application Development
- Stefan Buttigieg Milorad Jevdjenic
- 458字
- 2021-07-09 21:21:33
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.
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.
- ASP.NET Core 5.0開發(fā)入門與實戰(zhàn)
- Mastering Articulate Storyline
- JavaScript動態(tài)網(wǎng)頁開發(fā)詳解
- bbPress Complete
- Salesforce Reporting and Dashboards
- Python爬蟲、數(shù)據(jù)分析與可視化:工具詳解與案例實戰(zhàn)
- OpenCV with Python Blueprints
- Machine Learning for OpenCV
- Mastering OAuth 2.0
- FusionCharts Beginner’s Guide:The Official Guide for FusionCharts Suite
- 量子計算機(jī)編程:從入門到實踐
- 深入大型數(shù)據(jù)集:并行與分布化Python代碼
- Python繪圖指南:分形與數(shù)據(jù)可視化(全彩)
- INSTANT EaselJS Starter
- Java Web程序開發(fā)參考手冊