MongoDB 4 Quick Start Guide
上QQ阅读APP看书,第一时间看更新

References

Using references (https://docs.mongodb.com/manual/reference/database-references/), it is possible to create a series of related collections in order to establish a normalized (https://docs.mongodb.com/manual/core/data-model-design/#normalized-data-models) data model. In the following diagram, a normalized data model is established by defining a field purchId, which forms a reference between the Customer collection and Purchases. Further, the field itemId can serve as a reference between Purchases and Products:

By imposing an SQL-esque solution on a MongoDB dataset, however, you defeat the purpose of using a NoSQL database. Unless your database driver provides support for DBRefs (https://docs.mongodb.com/manual/reference/database-references/#dbrefs), which allows for an embedded link between collections, you are forced to write code to traverse the references manually, which introduces the very overhead you wanted to avoid by choosing MongoDB in the first place!

As of this writing, only half of the programming language drivers available provide support for DBRefs:
  • Provides DBRef support: C#, Java, Node.js, Perl, Python, Ruby
  • Does not provide DBRefs support: C, C++, Haskell, PHP, Scala