How to check if the collection is capped or not?

We can check whether the collection is capped or not with the isCapped() method in MongoDB. This method returns true is the specified collections capped collection. Otherwise, return, false.

Syntax:

db.Collection_name.isCapped()

Example:

db.student.isCapped()

After convert student collection to a capped:

Capped Collections in MongoDB

Capped collections are fixed-size collections means when we create the collection, we must fix the maximum size of the collection(in bytes) and the maximum number of documents that it can store. After creation, if we try to add more than documents to their capacity, it overwrites the existing documents. It supports high-throughput operations, that are useful when we insert and retrieve documents based on insertion order. The working of the capped collection is similar to circular buffers, which means once the fixed space is allocated for the capped collection, it creates/makes spaces for the new documents by overwriting the oldest documents in the given collection. Capped collection can also have _id field and by default, each _id field has an index.

Similar Reads

How to create a Capped collection?

We create the capped collections using the createCollection() method in MongoDB. When we create a new capped collection, we must specify the maximum size of the collection in bytes, which will MongoDB pre-allocate for the collection. The size of the capped collection contains a small amount of space for the internal overhead and if the value of the size field is less than or equal to 4096, then the capacity of the collection is 4096 bytes. Otherwise, MongoDB will automatically increase the size to make it an integer multiple of 256 and specify a maximum document count using the max field. If the given collection reaches the maximum size limit before it reaches the maximum document count, then MongoDB removes older documents to create space for the new document....

How to check if the collection is capped or not?

We can check whether the collection is capped or not with the isCapped() method in MongoDB. This method returns true is the specified collections capped collection. Otherwise, return, false....

How to convert a collection to a capped?

If there is an existing collection that we want to change it to capped, we can do it using the convertToCapped command. It changes the current collection to capped collection:...

Advantages of Capped Collections

To return documents in insertion order, queries do not need an index, since it provides greater insertion throughput. Capped collections allow changes that match the size of the original document to ensure that the document does not change its position on the disk. Holding log files is useful....

Disadvantages of Capped Collections

The update operation fails if the update of a document exceeds the original size of the collection. Deleting documents from a capped collection is not possible. If you want to delete all records from a capped collection, then use the given command:...

Contact Us