In MongoDB, data is organised into collections and documents.
Collection
A collection is a grouping of MongoDB documents. It’s the equivalent of a table in a relational database management system (RDBMS). Collections do not enforce a schema, meaning that documents within a collection can have different fields and structures.
Document
A document is a set of key-value pairs. It’s the basic unit of data in MongoDB, similar to a row in an RDBMS table. Each document in a collection can have a different structure, but they are typically related in some way. Documents are represented in BSON (Binary JSON) format, which is a binary-encoded serialization of JSON-like documents.
In the above example, we can explore the core elements of MongoDB. On the left panel, we observe the database hierarchy: the overarching entity is the bookstore, while within it lies the collection of books.
- On the left panel, the bookstore is a database;
- The books is a collection.
- In the collection, there are six Documents, each structured in JSON format. These documents contain various fields such as _id, title, author, pages, genres, and rating.
- Each document within the “books” collection can have different fields or structures. For example, the
title
is a String, thepages
is an Integer and thegenres
is an Array.