MongoDB: Documents Instead of Rows

MongoDB is a document database. A database is a program that stores data and helps you find it again later. Many databases keep data in tables, where every row has the same columns. MongoDB is different. It stores each record as a document, a group of fields written much like a JavaScript object. Here is one movie stored as a document:

{
  _id: 1,
  title: "Inception",
  director: "Christopher Nolan",
  year: 2010,
  genres: ["sci-fi", "thriller"]
}

Each line inside the braces is a field: a name, a colon, and a value. A value can be a number or some text. It can also be a list, like genres. A list in square brackets is called an array. A value can even be another document. You will work with movie documents like this one for the whole course.

Documents are kept in collections. This movie belongs in a collection called movies, and a database holds many collections. If you have used a table database like MySQL, a collection is like a table, a document is like a row, and a field is like a column.

There is one big difference. A collection does not force its documents to have the same fields. One movie can have a field that the other movies do not have. Later lessons show when this helps and when it causes problems.

Every document has an _id field. No two documents in a collection can have the same _id, so the _id points to exactly one document.

The first exercise is already written for you, so just hit Submit. After that, answer one question about documents and collections.