Collections in MongoDB
Collections in MongoDB
In MongoDB, a collection is a grouping of MongoDB documents, analogous to a table in a relational database. However, unlike tables that enforce a strict schema (structure of rows and columns), MongoDB collections are schema-flexible, allowing documents within a collection to have different fields and data types.
Key Characteristics of Collections in MongoDB
1. No Fixed Schema
- Unlike relational databases, where every row in a table must follow a predefined structure, MongoDB collections are schema-less. This means documents within the same collection do not need to have the same structure.
- One document might have a certain set of fields, while another in the same collection could have a completely different set of fields.
Example:
{
"_id": ObjectId("60f76c5917a1f4b4e71d3a8b"),
"name": "Alice",
"email": "alice@example.com"
}
{
"_id": ObjectId("60f76c5917a1f4b4e71d3a8c"),
"username": "bob99",
"age": 25,
"address": {
"city": "New York",
"state": "NY"
}
}
Both documents are stored in the same collection but have different fields (name
, email
in one, username
, age
, address
in another).
2. Flexible Document Structure
- Documents in a collection are stored in BSON (Binary JSON) format, which allows for more data types than plain JSON.
- You can have different structures within the same collection, supporting complex and varying data models in a single system.
3. Implicit Collection Creation
- In MongoDB, you don't need to explicitly create a collection. A collection is created automatically when the first document is inserted into it.
- Example: If you try to insert a document into a collection that doesn’t exist, MongoDB will automatically create the collection.
db.newCollection.insert({ name: "John Doe", age: 30 });
// The `newCollection` will be automatically created if it doesn't exist.
4. Naming Collections
- Collection names are case-sensitive. For example,
Users
andusers
are treated as two different collections. - Collection names must follow these rules:
- Cannot be an empty string.
- Cannot include
null
characters. - Cannot start with the character
system.
(reserved for internal collections). - Maximum length of 128 characters.
5. Working with Collections
Collections are where all interactions with data in MongoDB happen. Some of the common operations involving collections include:
Inserting Documents
To insert data into a collection:
db.users.insertOne({
name: "John Doe",
email: "john@example.com"
});
- This command inserts a document into the
users
collection. If the collection doesn't exist, it will be created automatically.
Querying Documents
You can query documents from a collection using find()
and related methods.
db.users.find({ name: "John Doe" });
This query searches the users
collection for documents where the name
field matches "John Doe"
.
Updating Documents
You can update documents in a collection with the updateOne()
or updateMany()
methods.
db.users.updateOne(
{ name: "John Doe" },
{ $set: { age: 31 } }
);
This command updates a document in the users
collection where the name
is "John Doe"
and sets the age
to 31
.
Deleting Documents
You can delete documents from a collection using deleteOne()
or deleteMany()
methods.
db.users.deleteOne({ name: "John Doe" });
This deletes a single document where name
is "John Doe"
.