MongoDB vs. SQL/MySQL: Why and When to Use MongoDB
September 14, 2018
How to use MongoDB
September 14, 2018

What is a Collection in MongoDB?

MongoDB stores data in the form of documents, which are stored in collections, while collections are stored in the database. A collection can store a number of documents that might not be the same in structure. Since MongoDB is a Schema-free DB, a collection can store documents with varying structures.

Compared to other RDBMS (Relational Database Management Systems) such as MySQL in which the structure or organization of data is defined by a schema, MongoDB allows storing documents of different structures in the same collection. This means users don’t have to define columns and its datatype. Two MongoDB documents might belong to the same collection, but can store different data structures.

Rules for Naming a MongoDB Collection

The name of any MongoDB collection must begin with an underscore or letters and can also contain numbers. The character $ is reserved and cannot be used within the name of any DB collection, while the collection name should also not exceed 128 characters.

80-90 characters is considered the sweet spot for the number of characters one should use in naming a collection. Users also have the option of using a dot ‘.’ notation (known as collection namespace), allowing them to organize the collections in named groups.

Creating a MongoDB Collection

A MongoDB collection is created when a user inserts the first document. In other words, a user does not have to create a collection before inserting a document in it. A document can easily be inserted using a single command in the collection, which means MongoDB creates a collection on-the-fly.

db.collection_name.insert({key:value, key:value…})

Users can also create collections with specific options before inserting any document with the following command:

db.createCollection(name, options)

Name: Name of the collection

Options: Optional field allowing users to specify certain parameters such as the maximum number of documents and size in the collection

Capped MongoDB Collections

Capped collections allow logging activities happening with an app and storing data in the same order as they are inserted. Capped collections are fixed-size and high-performance (capped at 1×109 for 32 bit, for 64-bit machines only system resources limit the maximum size) as no ordering is required when retrieving the data.

Although the DB supports updating existing objects and creating new ones, users cannot remove individual objects from this type of collection. Instead, they have to recreate the collection. Capped collections are mainly used for caching, logging and auto-archiving as they cannot grow beyond the specified threshold (older documents are removed if the maximum size limit is reached).

MongoDB Collection with Document Validation

Document validation allows users to compare documents with specified criteria using the ‘validator’ option. The DB refuses to insert/update the document or returns a warning if specified criteria are not met.

Dropping a Collection in MongoDB

After connecting to the database (use database_name) from which a user intends to drop a collection, a collection can be deleted using the following function:

db.COLLECTION_NAME.drop()

The list of available collections can be verified using ‘show collections’ function

Dropping a collection drops all the associated indexes and documents. The ‘remove()’ function allows users to preserve indexes, which only removes the documents without removing the collection and its indexes.

Leave a Reply

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