Hope, you enjoyed our last tutorial of MongoDB Auto Increment Sequence. Now, we will be looking about MongoDB capped collection. These processes can support large throughput since they are fixed collections.

We can insert and retrieve a document based on insertion order. Moreover, we will learn how to create, convert, check and query a capped collection in MongoDB.

Finally, we'll look at the advantages and disadvantages of using MongoDB Capped Collection in applications.

MongoDB Capped Collections: What Are They?

These processes can support large throughput since they are fixed collections. We can insert and retrieve a document based on insertion order.

Similar to circular buffers, they operate by overwriting the collection's oldest documents when space is granted to a collection file.

It indicates that it starts eliminating the oldest document in the collection without providing any explicit orders. Capped collection restrict updates if the updates result in increased document size.

It keeps track of document size since it shouldn't increase the size allotted to disc and stores documents in the order of disc storage. It works best for storing log data, cache data, and other large amounts of data.

In MongoDB, how do you create a capped collection?

Using the standard createCollection command and setting the capped option to true and the collection's maximum size in bytes, we can create a MongoDB capped collection.

>db.createCollection("cappedLogCollection",{capped:true,size:10000})

In addition, we can use the max parameter to limit the number of documents in the collection.

>db.createCollection("cappedLogCollection",{capped:true,size:10000,max:1000})

How to Determine Whether or Not a Collection Is Capped ?

Use the following command to determine whether a collection is capped or not:

>db.cappedLogCollection.isCapped()

How to Convert a Collection to Capped?

If you have an existing collection that you want to modify to capped, use the following command:

>db.runCommand({"convertToCapped":"posts",size:10000})

How do I locate a query on a capped collection in MongoDB?

A find query on a capped collection will typically return results in insertion order. However, if you want the documents to appear in reverse order, use the sort command as shown below:

>db.cappedLogCollection.find().sort({$natural:-1}

MongoDB Capped Collection Facts

Here are some things to keep in mind concerning Capped Collections in MongoDB:

  1. Documents in a capped collection cannot be deleted. It can only be destroyed automatically when new documents are added to the collection and the collection's allocated size is reached.
  2. MongoDB returns the same document in the order in which it was present on disc after reading the documents from a capped collection. As a result, read operations are executed very quickly.
  3. The update operation has one limitation. If the collection update results in an increase in the size of the document, it will not update that document in the collection because each document has a set size for the first time insertion into the capped collection.

How to Uncapped a MongoDB Capped Collection?

Here, we shall use two commands: mongodump and mongorestore.

Dump the capped collection

First and foremost, we will remove the capped collection.

PROD root@myhost:/data/uncap # mongodump -d mydb -c myexample
connected to: 127.0.0.1:27017
Sun Feb 5 00:02:28.879 DATABASE: mydb to dump/mydb
Sun Feb 5 00:02:28.880 mydb.myexample to dump/mydb/myexample.bson
Sun Feb 5 00:02:31.004 Collection File Writing Progress: 868400/67569879 1% (objects)
...
Sun Feb 5 00:24:13.004 Collection File Writing Progress: 67480900/67569879 99% (objects)
Sun Feb 5 00:24:14.203 67569879 objects
Sun Feb 5 00:24:14.203 Metadata for mydb.myexample to dump/mydb/myexample.metadata.json

Metafile in JSON format

Dump now includes a JSON-format metadata file.

cat dump/mydb/myexample.metadata.json
  {
    "options": {
    "capped": true,
    "size": 107374182400
   },
    "indexes": [
      {
        "v": 1,
        "key": {
        "_id": 1
      },
      "ns": "mydb.mycoll",
      "name": "_id_"
    }
  ]
}

Remove ?option? section

After that, remove the "options" section that displays the collection size cap.

PROD root@myhost:/data/uncap # mongorestore -d mydb -c myexample_tmp dump/mydb/myexample.bson
connected to: 127.0.0.1:27018
Sun Feb 5 00:29:41.473 dump/mydb/myexample.bson
Sun Feb 5 00:29:41.473 going into namespace [mydb.myexample_tmp]
Sun Feb 5 00:29:44.060 Progress: 51202216/106169934834 0% (bytes)
Sun Feb 5 00:29:47.007 Progress: 106497873/106169934834 0% (bytes)
Sun Feb 5 01:57:19.065 Progress: 106159626025/106169934834 99% (bytes)
67569879 objects found
Sun Feb 5 01:57:19.637 Creating index: { key: { _id: 1 }, ns: "mydb.myexample_tmp", name: "_id_" }

Makes indexes for us

It then creates indices for us automatically. After that, double-check it.

rs-prod:PRIMARY> db.myexample.isCapped()
true
rs-prod:PRIMARY> db.myexample_tmp.isCapped()
false
rs-prod:PRIMARY> db.myexample.count()
9876543210
rs-prod:PRIMARY> db.myexample_tmp.count()
9876543210

Drop old collection and renaming

After that, delete the old collection and rename the new one.

db.myexample.drop()

db.myexample_tmp.renameCollection('myexample')
db.myexample.count()
1254789635

Benefits of Capped Collections

  • Queries do not require an index to return documents in insertion order, resulting in higher insertion throughput.
  • Capped collections allow updates that are the same size as the original document, ensuring that the document does not change location on disc.
  • Keeping log files is beneficial.

The Drawbacks of Capped Collections

  • If the size of a collection is exceeded while updating a document, the update action fails.
  • Documents from a capped collection cannot be deleted. Use the following command to remove all records from a capped collection:
{ emptycapped: nameOfCollection }
  • Sharding a capped collection is not possible.

Application of Capped Collection

  • Logging of applications
  • History
  • Data from audits

Conclusion

As a result, we've learned everything there is to know about MongoDB capped collections, including how to create, convert, and check them, as well as their benefits and drawbacks. Finally, we learned about mongodump and mongorestore, which are the two commands used to uncap the collection.


Recommended Posts

View All

How to Drop a MongoDB Database?


Dropping a database is straightforward and may be accomplished via three methods from the command line as well as any GUI (Graphical User Interface) t...

Defining, Creating and Dropping a MongoDB collection


MongoDB Create Collection, MongoDB Drop Collection, how to create and drop a collection in MongoDB, an example of a collection in MongoDB, MongoDB col...

10 Reasons to Study MongoDB in 2023


Why should we pick MongoDB, we'll discuss here. It has advantages and disadvantages of its own, just like every coin has two sides.

Top Features You Must Learn to Master in MongoDB


MongoDB is a NoSQL database with a tonne of amazing features. These incredible qualities give this technology a distinctive and alluring look. These c...

Learn What is MongoDB with Application and Features


MongoDB is a C++-based open source platform with a fairly simple setup process. It is a non-structured, document-oriented, cross-platform database. Hi...