Get to know MongoDB and how it stores structured aggregates as documents. Learn the difference between referencing and embedding associations. Finally, implement a task tracker and block-style wiki to train data modeling with documents.
Examples:
Exercises:
Context
Document databases typically store their data using hash tables or a Distributed Hash Table.
Traditional hash tables are data structures used to efficiently map keys to values. Each key is hashed using a hash function, which produces a unique identifier for the input. This identifier is then used to map the key to a specific slot in an array, where the corresponding value is stored.
When a key needs to be looked up, it is passed through the hash function to produce its identifier, which is then used to directly access the corresponding value in the array. This allows for constant-time lookups, regardless of the size of the data set. In contrast to distributed hash tables, traditional hash tables are typically implemented on a single machine and are not distributed across a network.
The aggregate value in a database can be either opaque or structured, depending on the type of database. In pure key-value databases (Redis), the aggregate value is an opaque blob of data with no inherent meaning. However, databases that support structured aggregates, such as document databases (MongoDB ) and wide-column stores (DynamoDB), use values with known and inspectable structures. This enables the database system to perform queries on a subset of an aggregate.
A document in a document-database is a structured aggregate. Often, documents belong to a collection, which is a grouping of documents. Here, we can see a collection of people documents:
From https://www.mongodb.com/docs/manual/core/databases-and-collections/
Typically, a collection does not enforce any schema or rules to documents. Each document can have a different schema. However, some document databases, such as MongoDB, allow to setup document validation rules which are enforced during update and insert operations. This is similar to a database schema as we know it from relational databases, but lacks the support for automatic migrations:
db.createCollection("students", {
validator: {
$jsonSchema: {
bsonType: "object",
title: "Student Object Validation",
required: [ "address", "major", "name", "year" ],
properties: {
name: {
bsonType: "string",
description: "'name' must be a string and is required"
},
year: {
bsonType: "int",
minimum: 2017,
maximum: 3017,
description: "'year' must be an integer in [ 2017, 3017 ] and is required"
},
gpa: {
bsonType: [ "double" ],
description: "'gpa' must be a double if the field exists"
}
}
}
}
})
Specify JSON Schema Validation
MongoDB is a source-available, document-oriented database that stores data in a flexible, JSON-like format called BSON. As document are structured aggregates, MongoDB supports indexing, querying and updating of sub-documents, making it very powerful.