codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

MongoDB

7 / 65 topics
6Data Modeling Concepts7Collections and Documents8Fields and Values9Data Types in MongoDB10Embedded References11Schema Design Principles
Tutorials/MongoDB/Collections and Documents
🍃MongoDB

Collections and Documents

Updated 2026-04-20
3 min read

Introduction

MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. Understanding how collections and documents work is fundamental to effectively using MongoDB. In this tutorial, we will explore the core concepts of collections and documents, including their structure, operations, and best practices.

What are Collections?

In MongoDB, a collection is a group of documents. Think of a collection as an equivalent to a table in a relational database. However, unlike tables, collections do not enforce a schema, allowing for greater flexibility in data storage.

Key Characteristics of Collections

  • Dynamic Schema: Documents within the same collection can have different structures.
  • Scalability: Collections can grow dynamically with the amount of data stored.
  • Indexing: Collections support indexing to improve query performance.

What are Documents?

A document in MongoDB is a basic unit of data. It is similar to a JSON object and consists of key-value pairs. Documents are stored as BSON (Binary JSON) format, which is an efficient binary representation of JSON-like documents.

Key Characteristics of Documents

  • Flexible Structure: Each document can have different fields.
  • Nested Documents: Documents can contain nested documents or arrays.
  • Embedded References: Documents can reference other documents within the same collection or in other collections.

Creating Collections and Documents

Creating a Collection

MongoDB automatically creates a collection when you insert the first document into it. However, you can explicitly create a collection using the createCollection method.

// Using MongoDB shell
db.createCollection("users");

Inserting Documents

Documents are inserted into collections using the insertOne or insertMany methods.

// Inserting a single document
db.users.insertOne({
  name: "John Doe",
  age: 30,
  email: "john.doe@example.com"
});

// Inserting multiple documents
db.users.insertMany([
  { name: "Jane Smith", age: 25, email: "jane.smith@example.com" },
  { name: "Alice Johnson", age: 35, email: "alice.johnson@example.com" }
]);

Querying Documents

MongoDB provides a powerful query language to retrieve documents from collections. Basic queries can be performed using the find method.

// Finding all documents in the users collection
db.users.find();

// Finding documents with specific criteria
db.users.find({ age: { $gt: 30 } });

// Using projection to limit fields returned
db.users.find({}, { name: 1, email: 1 });

Advanced Queries

MongoDB supports advanced querying features such as sorting, limiting, and aggregation.

// Sorting documents by age in descending order
db.users.find().sort({ age: -1 });

// Limiting the number of results returned
db.users.find().limit(2);

// Using aggregation framework for complex queries
db.users.aggregate([
  { $match: { age: { $gt: 30 } } },
  { $group: { _id: "$age", count: { $sum: 1 } } }
]);

Updating Documents

Documents can be updated using the updateOne or updateMany methods.

// Updating a single document
db.users.updateOne(
  { name: "John Doe" },
  { $set: { age: 31 } }
);

// Updating multiple documents
db.users.updateMany(
  { age: { $lt: 30 } },
  { $inc: { age: 1 } }
);

Deleting Documents

Documents can be deleted using the deleteOne or deleteMany methods.

// Deleting a single document
db.users.deleteOne({ name: "Jane Smith" });

// Deleting multiple documents
db.users.deleteMany({ age: { $gt: 35 } });

Best Practices

Use Meaningful Collection Names

Collection names should be descriptive and follow a consistent naming convention. For example, use users, products, or orders.

Keep Collections Small

While MongoDB is designed to handle large datasets, it's generally best practice to keep collections small for better performance and manageability.

Index Your Collections

Indexing is crucial for improving query performance. Identify frequently queried fields and create indexes on them.

// Creating an index on the email field
db.users.createIndex({ email: 1 });

Use Embedded References Wisely

Embedded references can improve read performance but may complicate updates. Consider the trade-offs when designing your data model.

Regularly Monitor and Optimize

Use MongoDB's built-in tools to monitor performance and optimize queries as needed.

Conclusion

Understanding collections and documents is essential for effectively using MongoDB. By leveraging the flexibility of JSON-like documents and the power of MongoDB's query language, you can build scalable and efficient applications. Follow best practices to ensure optimal performance and maintainability.

This tutorial provides a comprehensive overview of collections and documents in MongoDB. For more advanced topics, consider exploring MongoDB's aggregation framework, sharding, and replication features.


PreviousData Modeling ConceptsNext Fields and Values

Recommended Gear

Data Modeling ConceptsFields and Values