Month End Sale: Get Extra 10% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now

Collections and Documents in MongoDB

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:08:00

What is a Collection in MongoDB?

A collection is a set of MongoDB documents that are grouped. It is similar to a table in a relational database.

Features of Collection

  • Collections contain individual MongoDB documents, which are JSON-like data structures.
  • Documents within a collection can have different structures than tables in SQL databases.
  • Collections are schema-free, allowing for more freedom in data modeling.

Creation of Collection

When the first document is added to a collection, it is automatically generated. The db.createCollection() method can also be used to manually create collections.

Collection Naming Conventions

  • Names are case-sensitive and must not include the null character.
  • Names should start with an underscore, letter, or dollar symbol.
  • Certain names (such as "system") are reserved.

Operations Performed on Collections

  • Insert: Documents are added to collections using the insertOne(), insertMany(), or insert() methods.
  • Query: To retrieve documents, use techniques such as find() or specialized queries such as aggregate().
  • Update: Change documents with methods such as updateOne(), updateMany(), or replaceOne().
  • Delete: Delete documents with the deleteOne() or deleteMany() methods.
  • Indexing: Collections can be indexed to improve query performance.

What is a Capped Collection?

A capped collection has a fixed size and can handle high-throughput activities. The system functions similarly to a circular buffer, adding new documents by overwriting the oldest ones when a collection reaches its given space. You cannot take a document out of a capped collection.


Creating a Capped Collection

  • A capped collection can be created using the createCollection() method.
  • MongoDB pre-allocates space based on the maximum size specified in bytes.
  • If the size is less than 4096, the capacity is 4096 bytes; otherwise, the size is expanded to a multiple of 256.
  • The max field allows you to choose the maximum document count.
  • If the collection exceeds its size limit before the document count, MongoDB eliminates older documents.
  • MongoDB automatically maintains space in capped collections by eliminating obsolete documents.

Parameters for Collection Creation

  • name: This is the collection's name and is a string.
  • options: This parameter is optional.
  • capped: It is used to generate a capped collection. If the value of this field is true, you must enter the maximum size in the size field. It is a boolean type.
  • autoIndexId: If set to false, this property disables the automatic construction of an index on the _id field. It is a boolean type.
  • size: It defines the collection's maximum size in bytes. It is a numerical type.
  • max: It specifies the maximum number of documents permitted in the capped collection. It is a numerical type.
  • storageEngine: It only works with the WiredTiger storage engine. It is a document type.
  • validator: It defines the collection's validation rules. It's a document.
  • validationLevel: This shows the extent to which MongoDB enforces the validation rule on the current document when it is being updated. It is a string type.
  • validationAction: It indicates if incorrect documents should be rejected outright or if they should only be warned about and allowed to be inserted. It is a string type.
  • IndexOptionDefaults: When creating a collection, the user can select the default index settings. It is a document type.
  • viewOn: The name of the collection or view from which the view will be created. It is a string type.
  • pipeline: This is an array containing the aggregate pipeline phases. Alternatively, using this argument, we can apply the pipeline to the view or viewOne collection. It is an array type.
  • collation: It defines the default collation for the provided collection. It is of the document type.
  • writeConcern: This parameter expresses the write concern for the operation. If you want to use the default write concern, ignore this argument. It is a document type.

Checking Collection Capped Status

In MongoDB, we may use the isCapped() method to determine whether or not a collection is capped. This function returns true if the supplied collection is a capped collection. Otherwise, return false.

Converting a Collection to a Capped Collection

If we want to change an existing collection to capped, we can do so with the convertToCapped command. It converts the current collection to a capped collection.

Capped Collections Advantages

  • Insertion order retrieval does not require an index, allowing for maximum insertion throughput.
  • Capped collections keep insertion order, which optimizes for chronological data.
  • The basic FIFO (First-In-First-Out) characteristic of log files makes them suitable for storage.

Capped Collections Disadvantages

  • If the document being updated exceeds the collection's original size, the update operation fails.
  • Documents cannot be deleted from a capped collection.
  • Capped collections cannot be sharded.

Capped Collection Usages

  • Logging (Activity logging)
  • Caching (Preserving the latest items)
  • Acting as a queue

What is a Document?

When a document is referred to for the first time, it is constructed on the fly and includes a _id field, similar to an RDBMS primary key. They allow Relationships via Embedded or References and store data in BSON, the binary equivalent of JSON.

Characteristics of Document

  • Flexible Schema: The MongoDB document model supports flexible and dynamic schemas, which means that each document in a collection can have its distinct structure.
  • BSON Format: MongoDB internally stores documents in BSON (Binary JSON) format, allowing for efficient data storage and manipulation.
  • Nested Structures: Documents can have nested structures, such as arrays and sub-documents, which allows for more complicated data models.

Working with Documents in MongoDB

Creating Documents

To create documents in MongoDB, use the insertOne() or insertMany() methods. These methods allow you to add one or many documents to a collection, respectively.

Querying Documents

MongoDB offers strong querying techniques, such as find() and findOne(). MongoDB's query operators allow you to query documents based on particular requirements, such as equality, range, or even complex conditions.

Updating Documents

MongoDB has methods such as updateOne() and updateMany() for updating existing documents in a collection. You can specify the update operation with a variety of update operators, including $set, $inc, $push, and so on.

Deleting Documents

To delete documents from a collection, use either the deleteOne() or deleteMany() methods. These methods allow you to remove one or more documents that meet specific filtering criteria.

Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this