MongoDB Capped Collection – Create, Check, & Uncapping the Capped

Hope, you enjoyed our last tutorial of MongoDB Auto Increment Sequence. Now, we will be looking at MongoDB capped collection. These are fixed collection, which supports high throughput operations. On the basis of an insertion order, we can insert and retrieve a document. Moreover, we will learn how to create, convert, check and query a capped collection in MongoDB.

At last, we will see what are the pros and cons with applications of MongoDB Capped Collection.

What is MongoDB Capped Collection

MongoDB Capped Collection – Create, Check, Convert, Uncapping the Capped

1. What is MongoDB Capped Collections?

These are fixed collection, which supports high throughput operations. On the basis of an insertion order, we can insert and retrieve a document. They work similar to circular buffers: once a collection file is allocated space, it makes room for new documents by overwriting the oldest documents in the collection.

It means that it starts deleting the oldest document in the collection without providing any explicit commands. Capped collection restrict updates if the updates result in increased document size. It stores documents in the order of disk storage, so it keeps a check on the document size as it should not increase the size allocated to disk. It is best for storing log information, cache data or any other high volume data.

i. How to Create Capped Collection in MongoDB?

To create a MongoDB capped collection, we use the normal createCollection command with a capped option as true and specifying the maximum size of the collection in bytes.

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

Along with is, we can limit the number of documents in the collection using max parameter-

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

Do you know how to create dataBase in MongoDB?

ii. How to Check Collection is Capped or not?

If you want to check whether a collection is capped or not, then use the following command:

>db.cappedLogCollection.isCapped()

iii. How to Convert a Collection to Capped?

If there is an existing collection which you want to change it to capped, you can do it by using the following command:

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

iv. How to find Query on Capped Collection in MongoDB?

Generally, a find query on a capped collection will display results in insertion order. But if you want the documents in reverse order, use sort command as shown below:

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

2. Facts about MongoDB Capped Collection

Following are some points to remember about Capped Collections in MongoDB:

  1. You cannot delete documents from a capped collection. It can only be deleted automatically upon insertion of new documents when the allocated size to the collection has been exhausted.
  2. After reading the documents from a capped collection, MongoDB returns the same document in the order which they were present on disk. Because of this, it makes the read operations to execute very fast.
  3. Update operation has one restriction with itself. If the update in collection results in the increase of the document’s size, then it will not update that document in the collection as each document has it’s fixed size during the first time insertion into the capped collection.

Recommended Readng – How to Update Document in MongoDB?

3. How to Uncapped a MongoDB Capped Collection?

Here we will be using two commands that are mongodump and mongorestore.

i. Dump the capped collection

First of all, we will dump 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

ii. Metafile in JSON format

Now, dump also includes a metadata file in JSON format.

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

Have a look – MongoDB Backup & Restore Option

iii. Remove “option” section

Now after this remove the “options” section that displays the capped collection size.

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_" }

iv. Creates indices for us

After this it automatically creates indices for us. Check it after this.

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

v. Drop old collection and renaming

After this drop the old collection and rename the new collection.

db.myexample.drop()

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

4. Advantages of Capped Collections

These are the some benefits:

  • Queries don’t need an index to return documents in insertion order due to which it provides higher insertion throughput.
  • Capped collections allow updates that fit the original document size, which gives us assurance that the document does not change its location on disk.
  • It is useful to keep log files.

5. Disadvantages of Capped Collections

Following are the limitations of capped collections in MongoDB:

  • If updating a document exceeds the original size of a collection, the update operation fails.

Also, Learn – MongoDB Relationships & Database Reference 

  • It is not possible to delete documents from a capped collection. To remove all records from a capped collection, use the following command:
{ emptycapped: nameOfCollection }
  • It is not possible to shard a capped collection.

5. Application of Capped Collection

Some of the applications of a capped collection are as follows:

  • Application logging
  • History
  • Audit information

6. Summary

Hence, we have studied all about MongoDB capped collections – how to create, convert, check, advantages, disadvantages. At last we studied about mongodump and mongorestore are the two commands use for uncapping the collection.

Hope. you find it useful. If you have any query, feel free to share with us. Surely, we will get back to you!

Related Topic – MongoDB ObjectId & Atomic Operations

Leave a Reply

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