</>
Skip to content
MongoDB lessons (6/38)

MongoDB — Collections

Create Collection

db.createCollection("users")

List Collections

show collections

Drop Collection

db.users.drop()

Collection Operations

// Rename collection
db.users.renameCollection("customers")

// Get collection stats
db.users.stats()

Create with Options

// Capped collection
db.createCollection("logs", {
  capped: true,
  size: 1024 * 1024,
  max: 1000
})

// Validator
db.createCollection("users", {
  validator: {
    $jsonSchema: {
      bsonType: "object",
      required: ["name", "email"],
      properties: {
        name: { bsonType: "string" },
        email: { bsonType: "string" }
      }
    }
  }
})

Collection Methods

MethodDescription
createCollectionCreate new
dropRemove collection
renameCollectionRename collection
statsGet statistics

Mini Practice

  1. Create a collection
  2. List all collections
  3. Drop a collection
  4. Create with validator

Up Next

Continue with Documents — working with documents.

Related Topics

Frequently Asked Questions about Collections

What is Collections in MongoDB?

Collections is a fundamental concept in MongoDB. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Collections?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Collections.

Why is Collections important in MongoDB?

Collections is essential for MongoDB development. Understanding this concept will help you write better code and solve real-world problems more effectively.