Visit complete MongoDB roadmap

← Back to Topics List

Collections and Methods

In MongoDB, collections are used to organize documents. A collection can be thought of as a container or group used to store documents of similar structure, like a table in relational databases. However, unlike tables, collections don’t enforce a strict schema, offering more flexibility in managing your data.

Key Features

  • Flexible Schema: A collection can contain multiple documents with different structures or fields, allowing you to store unstructured or semi-structured data.
  • Dynamic: Collections can be created implicitly or explicitly, and documents can be added or removed easily without affecting others in the collection.

Creating Collections

To create a collection in MongoDB, you can choose from two methods:

  • Implicit Creation: When you insert a document without specifying an existing collection, MongoDB automatically creates the collection for you.

  • Explicit Creation: Use the db.createCollection(name, options) method to create a collection with specific options:

    db.createCollection('users', { capped: true, size: 100000, max: 5000 });

Managing Collections

  • Insert Documents: To insert a document into a collection, use the insertOne() or insertMany() methods.

    db.users.insertOne({ name: 'John Doe', age: 30, email: '' });
      { name: 'Jane Doe', age: 28, email: '' },
      { name: 'Mary Jane', age: 32, email: '' },
  • Find Documents: Use the find() method to query documents in a collection.

    db.users.find({ age: { $gt: 30 } });
  • Update Documents: Use the updateOne(), updateMany(), or replaceOne() methods to modify documents in a collection.

    db.users.updateOne({ name: 'John Doe' }, { $set: { age: 31 } });
    db.users.updateMany({ age: { $gt: 30 } }, { $inc: { age: 1 } });
  • Delete Documents: Use the deleteOne() or deleteMany() methods to remove documents from a collection.

    db.users.deleteOne({ name: 'John Doe' });
    db.users.deleteMany({ age: { $lt: 30 } });
  • Drop Collection: To delete the entire collection, use the drop() method.


In summary, collections are an essential part of MongoDB that enable you to efficiently manage and store documents with varying structures. Their flexible schema and dynamic nature make them perfect for handling both unstructured and semi-structured data.

Community is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Roadmaps Best Practices Guides Videos Store YouTube by Kamran Ahmed

Community created roadmaps, articles, resources and journeys to help you choose your path and grow in your career.

© · FAQs · Terms · Privacy


The leading DevOps resource for Kubernetes, cloud-native computing, and the latest in at-scale development, deployment, and management.