Mastering MongoDB Collections
Tags: mongodb, beginners, programming, database
Published: 2023-08-15 20:13:52

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:

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

{
  "name": "Database Systems",
  "dept": "Computer Science", 
  "prereqs": ["Data Structures"],
  "credits": 3
}
```

And in the `students` collection:

```json
{
  "name": "Jane Doe",
  "studentId": "101", 
  "major": "Computer Science",
  "enrollments": ["Data Structures", "Database Systems"]   
}
```

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:

```js
db.createCollection('students')
```

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:

```javascript
> show collections
courses
students
```

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:

```js
db.logs.drop()
```

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](https://mohitdotexe.hashnode.dev/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:

```js
db.createCollection('logs', {capped: true, size: 10000})
```

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
    

```js
db.createCollection('logs', {
  capped: true, 
  size: 10000,
  max: 100
})
```

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

> <mark>Note:</mark>  
> 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.
>     

```javascript
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
    })
```

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.

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

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.](https://www.mongodb.com/docs/manual/core/schema-validation/handle-invalid-documents/#std-label-schema-validation-handle-invalid-docs)

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:

```javascript
db.students.getIndexes()
[
  {
    "v" : 2,
    "key" : { "_id" : 1 },  
    "name" : "_id_" 
  }
]
```

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:

```javascript
db.createCollection('data', {autoIndexId: false})
```

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](https://dev.to/mohitsinghchauhan/learn-mongodb-in-one-shot-ultimate-mongodb-guide-4k82) - 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!