MongoDB Collection

MongoDB Collection is a group of MongoDB documents stored inside a database. It is similar to a table in MySQL, but instead of fixed rows and columns, a collection stores flexible JSON-like documents.

MongoDB Collection

A MongoDB database can contain many collections. Each collection usually represents one type of data in an application, such as users, products, orders, invoices, students, or employees. Every MongoDB Document stored in a collection is made of field-value pairs.

MongoDB Collection Compared with a Relational Table

In a relational database, a table normally has a predefined structure. All rows follow the same column layout. In MongoDB, a collection does not require every document to have exactly the same fields. This makes collections useful when application data can evolve over time.

Relational database termMongoDB termMeaning
DatabaseDatabaseContainer for data
TableCollectionGroup of related records or documents
RowDocumentSingle data record
ColumnFieldNamed data value inside a record

Schema Flexibility in a MongoDB Collection

Unlike relational databases, MongoDB collections do not require a rigid schema by default. Documents in the same collection can have different fields. For example, one user document may contain a phone number while another user document may not.

</>
Copy
db.users.insertOne({
  name: "Arun",
  email: "arun@example.com"
})

db.users.insertOne({
  name: "Meena",
  email: "meena@example.com",
  phone: "9876543210",
  city: "Hyderabad"
})

Both documents can exist in the same users collection. This flexibility is useful, but it should be used carefully. In most real applications, documents in a collection should still follow a consistent structure so that queries, indexes, validations, and application code remain simple.

MongoDB also supports schema validation when you want to enforce rules on a collection. For example, you can require a field, restrict a field type, or reject documents that do not match the expected structure.

How to Select a MongoDB Database and Use a Collection

In mongosh, you first switch to a database with the use command. Then you can work with a collection through the db.collectionName syntax.

</>
Copy
use school

db.students.insertOne({
  name: "Ravi",
  class: 10,
  marks: 86
})

In this example, school is the database and students is the collection. If the collection does not already exist, MongoDB can create it automatically when the first document is inserted.

Creating a MongoDB Collection Explicitly

You do not always need to create a collection manually. MongoDB can create it during the first insert operation. However, you may create a collection explicitly when you want to define collection options, validation rules, capped collection behavior, or other settings before inserting data.

</>
Copy
db.createCollection("students")

After creating or inserting into a collection, you can list collections in the current database.

</>
Copy
show collections
students

Basic Operations on Documents in a MongoDB Collection

Most work with a MongoDB collection is done through document operations. You insert documents, find documents, update matching documents, and delete documents from the collection.

</>
Copy
// Insert a document
db.students.insertOne({ name: "Ravi", class: 10, marks: 86 })

// Find all documents in the collection
db.students.find()

// Find documents that match a condition
db.students.find({ class: 10 })

// Update one matching document
db.students.updateOne(
  { name: "Ravi" },
  { $set: { marks: 90 } }
)

// Delete one matching document
db.students.deleteOne({ name: "Ravi" })

The collection name in these examples is students. In your application, choose a collection name that clearly describes the type of documents stored in it.

Viewing the Structure of a MongoDB Collection

MongoDB does not provide a table-style DESCRIBE command like SQL databases because a collection does not have a fixed column definition by default. To understand the structure of a collection, you can inspect sample documents, review the application code that writes to the collection, or check any validation rules defined for the collection.

</>
Copy
db.students.findOne()
{
  _id: ObjectId("..."),
  name: "Ravi",
  class: 10,
  marks: 90
}

The findOne() method shows one document from the collection. This is often the quickest way to understand the fields used in a small collection. For production data, inspect more than one document because different documents may contain different fields.

MongoDB Collection Naming Guidelines

A collection name should be simple, readable, and consistent across the application. Use names such as users, orders, products, or students. Avoid names that are too vague, such as data or items, because they make the database harder to understand later.

  • Use one naming style across the project, such as lowercase plural names.
  • Choose names based on the document type stored in the collection.
  • Do not create separate collections for data that should be queried together unless there is a clear design reason.
  • Keep collection names understandable for both developers and database administrators.

When to Use Separate MongoDB Collections

Create a separate MongoDB collection when the documents represent a different type of entity or when they need different indexes, validation rules, lifecycle rules, or access patterns. For example, an online store may use separate collections for customers, products, and orders.

Do not split data into too many collections only to imitate relational tables. MongoDB data modeling depends on how the application reads and writes data. Sometimes related information can be embedded inside one document; in other cases, it is better to keep it in a separate collection and reference it.

Operations on a MongoDB Collection

MongoDB Collection FAQs

What is a collection in MongoDB?

A collection in MongoDB is a group of documents stored inside a database. It is similar to a table in a relational database, but its documents can have flexible fields.

Is a MongoDB collection the same as a table?

A MongoDB collection is similar to a table because both store related records. The main difference is that a relational table usually has fixed columns, while a MongoDB collection stores documents that can have different fields.

Does MongoDB create a collection automatically?

Yes. MongoDB can create a collection automatically when you insert the first document into a collection name that does not already exist. You can also create a collection explicitly with db.createCollection().

Can documents in the same MongoDB collection have different fields?

Yes. Documents in the same collection can have different fields by default. However, using a consistent structure is usually better for application code, indexing, validation, and maintenance.

How do I see the fields in a MongoDB collection?

You can inspect sample documents using methods such as findOne() or find(). MongoDB does not have a direct SQL-style DESCRIBE command for collections without a fixed schema.

Editorial QA Checklist for MongoDB Collection Tutorial

  • Check that the tutorial explains database, collection, document, and field with correct MongoDB terminology.
  • Confirm that examples use mongosh syntax and valid collection operations.
  • Verify that schema flexibility is explained without implying that structure and validation are never needed.
  • Ensure code blocks use the correct PrismJS classes for JavaScript syntax and output blocks.
  • Confirm that collection creation, listing, querying, updating, and deleting are covered at an introductory level.