RavenDB 2.x  Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Anatomy of a Document

In RavenDB, data is stored as Documents and must be serialized from the native .NET objects to the JSON format before being sent to RavenDB database engine, they are later deserialized from JSON, and then to native .NET objects.

The flexible document-based structure reduces the need to worry about the structure of the data, either before or during the application development. You do still have to think about the structure of data in terms of identifying natural transactional boundaries (which in this case is the Document).

This helps in mapping the data and allow querying, combining, and filtering the information. Replication is also easy-to-use so you can copy, share, and synchronize your data between databases, machines, and even continents.

In a typical database, we store different related pieces of data. We might have an object and we might model this object using multiple objects. Take the example of a desktop computer in a company with different softwares installed by a user. We want to keep track of all these installed softwares. By modeling the Computer entity and its related Inventory, we will have a tree of related objects. Usually, in traditional RDBMS, we will store each one object as a part of the data tree in a table in the database and relate them together.

In a Document type database we skip the task of splitting the model in smaller entities and the entire structure will remain as a one single object. This object is stored in the database using a single REST verb (POST or PUT) and there is no need to split the object, or separate its data into primary entities. When we want to retrieve the object, we retrieve it entirely and we do not care where the data went or how to join it from different entities or documents.

Document-oriented databases are not limited to just storing keys and values. Instead, you can store complex object graphs as a single document. In a relational database, a row can only contain simple values and more complex data structures need to be stored as relations. In RavenDB, a Document can handle complex structure and we can store all the data that we need to work with as a single document.

Tip

To enhance the overall performance of your system, it is a good practice to include all of the information you need in a single document when using RavenDB and you are encouraged to do so.

At a collection level, this allows for putting together a diverse set of documents into a single collection. Document databases allow indexing of documents on the basis of not only its primary identifier but also its properties.

Note

Do not confuse "Document-oriented databases" and "Document Management Systems". The Document-oriented databases connotes loosely structured sets of key-value pairs in documents and a Document Management System is a computer system used to track and store documents.

Each RavenDB document contains data (other than yours) that describes other data (metadata), which aren't a direct part of the document. Metadata provides information about the CLR-type, the entity-name, the last-modified date, and so on. These metadata are attached to the document and are being used internally; but they can be exposed to your code.