What is Collection in MongoDB?

Connect with

collection in mongodbA collection in MongoDB is a group of documents. Collection in MongoDB is similar to Table in SQL relational database. Collection in MongoDB is a basic building block, which holds the same group of documents.

You can visit my previous article “What is a document in MongoDB?” if you would like to know details about documents in MongoDB.

1. Overview of Collection in MongoDB

You can keep the same group of documents in MongoDB, in principle, but you can keep a different group of documents in a collection. Ideally, you should keep the same group of documents in one collection. Keeping the same group of documents is always preferable for various reasons. If you want to know more about JSON documents you can visit  JSON document.

A collection in MongoDB is analog to a table in a relational SQL database, similarly, a document in MongoDB is analog to a row in a relational SQL database. The existence of collection in a database is not required before inserting a document in MongoDB. From your programming language, when you add the first document in a collection and the provided collection does not exist in that database then MongoDB creates a new collection. So you can say, for insertion of the document, collection need not be present in MongoDB.

You can create a collection in MongoDB by using the following command.
The syntax for creating a collection in MongoDB

use blog;
db.createCollection(name, options)

example of creating a collection in mongoDB

use blog;
db.createCollection("post", options)

2. Types of MongoDB Collection

In MongoDB there are various type of collections:

  • Normal collection for holding a normal collection
  • Capped Collection for Queue like data
  • TTL collection for caching data
  • system collection, this is a predefined collection
  • GridFS collection for large file data storage
  • indexed collection
  • Geo spatial collection for lat and long or 2D data or spherical data
  • full-text search collection for simple text/string search

The syntax for creating a capped collection in mongoDB

use blog;
db.createCollection(name, options)

example of creating a capped collection in mongoDB

use blog;
db.createCollection( "author",
     capped: true,
     autoIndexId: true,
     size: 100,
     max: 50

3. MongoDB collection and Dynamic Schema

If you think carefully, you can found every row can have different types of documents, in this way MongoDB does not need collection. You can ask yourself, why should we use more than one collection. If you think from a developer and database admin perspective, you can find the answer, if you keep all the documents in one collection, logically you can keep but management became hell in this way. In your Programming language, you have to write multiple loops to check this document belongs to which group, etc.

There are a few good reasons regarding, why we required collection in MongoDB.

  • grouping of the document in one collection
  • Fater to get a group of the document from the collection
  • data locality for the same type of document
  • Indexing more efficiently by MongoDB engine

4. Naming convention of MongoDB Collection

  • The name of the collection will be a UTF-8 character string.
  • Empty string i.e. “” is not a valid collection name
  • collection name must not contain null character i.e. \o , because this char is used for end of key
  • collection name will not start with system, because system. is a reserved keyword, e.g. system.users hold user database.
  • collection in MongoDB is unique, likewise, the table in the database is unique.
  • There can be multiple collections in a database.

5. Video version of this post: MongoDB collection

MongoDB Collections

6. Reference

MongoDB official site
You reached this point it means you like this post. Please write your comment which is valuable for me to improve this post. Please share your learning about collection in MongoDB. Happy Learning! 🙂

Connect with

Leave a Comment

Your email address will not be published. Required fields are marked *