DEV Community

Cover image for Mastering MongoDB Collections
MohitSinghChauhan
MohitSinghChauhan

Posted on • Originally published at mohitdotexe.hashnode.dev

Mastering MongoDB Collections

MongoDB stores your data in collections - groups of documents that allow you to organize and query your data efficiently.

Getting to grips with collections is key to leveraging the full power of MongoDB. This in-depth guide will cover everything you need to know.

Why Collections?

Suppose you are building an application to track courses and students at a university. You may store your data in two collections - courses and students.

Some example documents in the courses collection:

{
  "name": "Data Structures",
  "dept": "Computer Science",
  "credits": 3  
}

{
  "name": "Database Systems",
  "dept": "Computer Science", 
  "prereqs": ["Data Structures"],
  "credits": 3
}
Enter fullscreen mode Exit fullscreen mode

And in the students collection:

{
  "name": "Jane Doe",
  "studentId": "101", 
  "major": "Computer Science",
  "enrollments": ["Data Structures", "Database Systems"]   
}
Enter fullscreen mode Exit fullscreen mode

Organizing the related data into logical collections makes querying and managing the data intuitive.

Creating Collections

To create a new collection, use the db.createCollection() method:

db.createCollection('students')
Enter fullscreen mode Exit fullscreen mode

This will create an empty collection named 'students'.

Naming Collections

  • Collection names cannot start with system. - this is reserved.

  • Avoid using the $ character in names.

Other than these, there are no specific restrictions on collection names in MongoDB. But some good practices apply:

  • Use meaningful, domain-driven names like users, products etc.

  • Use camelCase or PascalCase and avoid underscores or hyphens.

  • Keep names short but descriptive.

  • Use plural nouns for names and singular forms for documents.

Listing Collections

Use show collections to view existing collections:

> show collections
courses
students
Enter fullscreen mode Exit fullscreen mode

You can also query the system.namespaces collection to list user-defined collections.

Dropping Collections

To remove an entire collection including all its documents, use the drop() method:

db.logs.drop()
Enter fullscreen mode Exit fullscreen mode

If the collection does not exist, MongoDB will throw an error.

Collection Methods

Some useful collection methods and operations:

  • db.collection.find() - Queries documents in the collection

  • db.collection.insertOne() - Inserts a single document

  • db.collection.insertMany() - Inserts multiple documents

  • db.collection.updateOne() - Updates one document

  • db.collection.updateMany() - Updates multiple documents

  • db.collection.deleteOne() - Deletes one document

  • db.collection.deleteMany() - Deletes multiple documents

  • db.collection.createIndex() - Creates an index on a field

  • db.collection.drop() - Drops/deletes the collection

And many more!

To Understand them in depth, Read my blog on it - Learn MongoDB in One Shot - Ultimate MongoDB Guide

Capped Collections

Capped collections have a fixed size - they stop growing after reaching the configured size. Older documents age out and are overwritten in FIFO order.

They provide high performance for use cases like logs:

db.createCollection('logs', {capped: true, size: 10000})
Enter fullscreen mode Exit fullscreen mode

Capped collections automatically maintain insertion order and do not need indexes.

You can pass options to configure the capped collection on creation:

  • capped - Boolean to specify if the collection is capped. Capped collections have a fixed size.

  • size - Maximum size of a capped collection in bytes.

  • max - Maximum number of documents for a capped collection

db.createCollection('logs', {
  capped: true, 
  size: 10000,
  max: 100
})
Enter fullscreen mode Exit fullscreen mode

This caps the collection to 10KB and 100 documents. Older documents age out automatically making it suitable for log data.

Note:

The size argument is always required, even when you specify the max number of documents. MongoDB removes older documents if a collection reaches the maximum size limit before it reaches the maximum document count.

Collection Validation

You can define a JSON schema to validate documents during insertion and updates:

Collection Schema validation is most useful for an established application where you have a good sense of how to organize your data. You can use schema validation in the following scenarios:

  • For a users collection, ensure that the password field is only stored as a string. This validation prevents users from saving their password as an unexpected data type, like an image.

  • For a sales collection, ensure that the item field belongs to a list of items that your store sells. This validation prevents a user from accidentally misspelling an item name when entering sales data.

  • For a students collection, ensure that the gpa field is always a positive number. This validation catches typos during data entry.

When MongoDB Checks Validation

When you create a new collection with schema validation, MongoDB checks validation during updates and inserts in that collection.

When you add validation to an existing, non-empty collection:

  • Newly inserted documents are checked for validation.

  • Documents already existing in your collection are not checked for validation until they are modified.

const studentSchema = {
  validator: {
    $jsonSchema: {
      bsonType: 'object',
      required: ['name', 'studentId'],
      properties: {
        name: {
          bsonType: 'string' 
        },
        studentId: {
          bsonType: 'string',
          minLength: 5
        },
        gpa: {
          bsonType: 'number',
          minimum: 0,
          maximum: 4 
        }
      }
    }
  }
}

db.createCollection('students', studentSchema)

xxxxxxxxxxxxxxxxxxxxxxxxx OR xxxxxxxxxxxxxxxxxxxxxxxxxxx

db.createCollection('users', {
  validator: {
    $jsonSchema: {
          bsonType: 'object',
          required: ['name', 'studentId'],
          properties: {
            name: {
              bsonType: 'string' 
            },
            studentId: {
              bsonType: 'string',
              minLength: 5
            },
            gpa: {
              bsonType: 'number',
              minimum: 0,
              maximum: 4 
            }
          }
        }
      } // Document validator
    })
Enter fullscreen mode Exit fullscreen mode

This enforces that:

  • name and studentId are required string fields

  • studentId must be at least 5 characters

  • gpa must be a number between 0-4

Validation allows enforcing data consistency as applications and data volume scales.

Other options like validationAction allows configuring whether to error or warn on invalid documents.

db.createCollection('users', {
  validator: {...}, // Document validator
  validationLevel: 'strict', // Validation mode
  validationAction: 'warn' // Action on validation failure  
})
Enter fullscreen mode Exit fullscreen mode

The validationAction determines what happens when a document fails validation:

  • error - Insert/update will fail and an error will be thrown. This is the default.

  • warn - The operation will succeed but a warning will be logged.

By default MongoDB rejects the operation and does not write the document to the collection. Check Out this to know more.

Setting validation rules ensures consistency as your application scales.

Auto-Indexing

By default, MongoDB automatically creates an index on the _id field when a collection is created.

For example:

db.students.getIndexes()
[
  {
    "v" : 2,
    "key" : { "_id" : 1 },  
    "name" : "_id_" 
  }
]
Enter fullscreen mode Exit fullscreen mode

The _id index provides efficient lookup by primary key. But indexing has overhead, so it may be disabled for use cases like:

  • Bulk loading data where high write speed is needed

  • Prototyping where indexes aren't needed yet

You can disable auto-indexing on _id using:

db.createCollection('data', {autoIndexId: false})
Enter fullscreen mode Exit fullscreen mode

Later, you can manually create indexes as needed for production use.

Ending Note :)

Collections are a core building block of MongoDB. Mastering collections allows you to organize, manage, and query your MongoDB data efficiently.

With this foundation, you now have a solid grasp of how to work with MongoDB collections.

To learn more, I advise you to read MongoDB Official documentation it provides a wealth of resources to continue advancing your MongoDB skills. And the active community enables learning from MongoDB experts around the world.

I also recommend checking out my other blog post (0xUpdate It also featured on Pfficial Mongodb Newsletter) - Learn MongoDB in One Shot - for a comprehensive beginner's guide to MongoDB fundamentals.

I hope you enjoyed this in-depth exploration of MongoDB collections. Let e know in the comments what topics you would like covered next!

Top comments (0)