1. What is a document database?
Welcome to this second chapter! In the previous chapter, we learned the basics of key-value databases. In this chapter, we will discover document databases. Let's start!
2. Document database - overview
Document databases store data
3. Document database - overview
in documents.
4. Document database - overview
5. Document database - overview
6. Document database - overview
7. Document database - overview
Documents are grouped into collections.
8. Document database - overview
We can have
9. Document database - overview
multiple collections within a database. If you are familiar with relational databases,
we can say that documents are analogous to rows
and collections to tables.
10. Documents
A document is a set of key-value pairs.
The keys are strings
and the values can be numbers, strings, booleans, arrays or objects.
As document databases are schemaless, there is no need to specify the structure of the documents.
Documents can encode the data in JSON, BSON, YAML, or XML formats. Throughout this chapter, we will focus on JSON format, as it is the most popular.
11. Documents - JSON format
Here is an example of a document in JSON format. It stores the information of every user registered at Datazy. As we can see, data is organized in key-value pairs separated by commas.
The value for the "address" key is another document. Notice that this related information is embedded in the main document, avoiding the need to look for the address in another place.
The value for the "hobbies" key is an array. Its elements are separated by commas and surrounded by square brackets.
12. Documents - queries
Unlike key-value databases that we studied in the previous chapter, we can elaborate more complex queries with document databases. For instance, we can
get all the users who live in New York and like hiking,
get all the users older than 40,
or simply get all the user's data by a given user identifier.
We will see some query examples in the lesson dedicated to MongoDB.
13. Documents - polymorphic model
Document databases are polymorphic. It means that documents within the same collection don't need to have the same structure. For instance, these two documents describe Datazy users, but the information they have is different. The document on the right doesn't have the address information, but it has the date of birth.
14. Collections
As we said before, collections are sets of documents.
Usually, collections store the same type of entities, such as users, products, etc., but they don't mix different types of entities.
A good way to organize documents and collections is by thinking about the queries we want to get in our application. There are best practices of modeling, but they are out of the scope of this course.
15. Popular document databases
Here are some popular document databases.
16. Let's practice!
Great job! Let's take some exercises before studying the advantages and limitations of document databases.