MongoDB: Understanding Documents and Collections for Beginners
MongoDB has emerged as a popular choice among developers who lean towards NoSQL databases.
If you dive into MongoDB for the first time, you might encounter some unfamiliar terminologies.
This article is meant to be a beginner-friendly guide where we'll break down two of the most essential terms: documents and collections.
Documents
Think of a document as an individual data entry in MongoDB.
This information is grouped in key-value pairs.
As an example: { "name": "Alice", "age": 30 }
.
If you are used to JavaScript, think of this as an Object
.
Each document gets a unique identifier called _id
. If you don’t specify one, MongoDB provides it automatically.
Collections
If documents are individual data entries, then collections are the folders or containers that hold them.
Typically a "collection" groups related documents. For instance, all data entries about users might be stored in a collection named "Users".
Unlike traditional databases, where each row in a table must have the same columns, MongoDB allows each document in a collection to have its own unique structure.
So, two documents in the same collection can have different keys. For example, a document might have a key for "age", while another might not.
Collections are housed within a database, and there can be multiple collections in a database.
If you are familiar with physical filing or even digital file storage, here's an analogy:
- A MongoDB document is like a single file on your computer.
- A MongoDB collection is like a folder containing multiple related files.
Happy coding! ✨