Document databases

The document model on MongoDB: JSON document, collection, identifier, nested fields, flexible schema and validation, and the choice between embedding and referencing.

  • 23 cards
  • 5 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios
  5. Understand

A few cards from the deck

  • Document database: what is it?

    Show the answer

    A database that stores self-contained records in JSON or a similar format, grouped in collections, where a record can hold sub-objects and arrays.

    An application object is stored in one piece, without being split across several tables: it is read and written in a single operation.

  • A blog stores 5,000 articles written by the same author. Which structure is best for the author?

    • A separate author document, pointed to by an authorId field
    • Copy the author's whole profile inside each of the 5,000 articles
    • Embed the 5,000 articles in the author's document
    • Use the author's name as the _id of each of the 5,000 articles
    Show the answer

    A separate author document, pointed to by an authorId field

    Copying the profile forces you to update 5,000 documents at the slightest change. Embedding the articles in the author makes one document grow toward its size limit. An _id is unique within its collection: 5,000 articles cannot share one. A reference keeps a single copy up to date.

  • In MongoDB, a set of documents is called a …; it is the equivalent of a relational …, with no structure imposed on each document.

    Show the answer

    In MongoDB, a set of documents is called a collection; it is the equivalent of a relational table, with no structure imposed on each document.

    Two documents of the same collection can have different fields. A collection is created on the fly at the first insertion.

  • Embedding or referencing: what is it?

    Show the answer

    The choice between copying a related piece of data inside the document that uses it, or keeping it in its own document and storing only its identifier.

    You embed what is always read with its parent and stays bounded; you reference what grows without limit, is shared by many documents or must stay up to date in one place.

  • Adding a follower fails: the account document exceeds the 16 MB maximum size, because its followers are stored in a nested array. What is the fix?

    • Compress the array before writing it into the document
    • Raise the maximum document size in the server configuration
    • A collection of subscriptions that reference the account
    • Put the followers in several nested arrays of the same document
    Show the answer

    A collection of subscriptions that reference the account

    The 16 MB limit is fixed and cannot be configured. An array that grows without bound must not be embedded: one document per subscription has no ceiling and is read page by page.