Capped Collection in MongoDB
Capped collections are fixed-sized Circular Collection that follow the insert order to enhance the performance of create, read, and delete operations. With Circular, it means that when fixed sizes are allocated for Collection, it will start deleting the oldest Document in that Collection without providing any explicit commands.
Capped Collection limits update activities to the Document if those updates increase the size of the Document. When Capped Collection stores documents in the order of Disk Storage, it ensures that the document size does not increase more than the size allocated on Disk. Capped Collection is best for storing log information, cache data, .
Create Capped Collection in MongoDB
To create a Capped Collection , we use the commonly used createCollection command but with the capped option true and specify the maximum size (in bytes) for that Collection.
> db . createCollection ( "cappedLogCollection" ,{ capped : true , size : 10000 })
With the size of the Collection, we can also limit the Document number in that Collection by using the max parameter:
> db . createCollection ( "cappedLogCollection" ,{ capped : true , size : 10000 , max : 1000 })
If you want to check if a Collection is capped or not, use the isCapped command:
> db . cappedLogCollection . isCapped ()
If there is an existing Collection that you intend to convert into capped, use the following code:
> db . runCommand ({ "convertToCapped" : "posts" , size : 10000 })
This code will convert existing posts collection into a Capped Collection.
Query on Capped Collection
By default, a find query on a Capped Collection will display the results in the insert order. But if you want to retrieve documents in reverse order, use the sort command as follows:
> db . cappedLogCollection . find (). sort ({ $natural :- 1 })
Here are some things to keep in mind while working with Capped Collection:
We cannot delete documents from a Capped Collection.
There is no default index present in a Capped Collection, even on the _id field.
While inserting a new Document, MongoDB doesn't really have to look at the location of the new Document on Disk. It can insert the new Document at the end of Collection. This makes the inserting of Capped Collection very fast.
Similarly, when reading documents, MongoDB only has to return documents in the same order as on Disk. This makes reading operations very fast.
According to Tutorialspoint
Previous post: GridFS in MongoDB
You should read it
May be interested
- Data type in MongoDBmongodb supports many different data types. the following are some typical data types.
- MongoDB malicious code attacks more than 26,000 victims in a weekmalware that attacks the mongodb database has rekindled last week and after the weekend with the arrival of three new groups hijack more than 26,000 servers, of which one group attacked 22,000 machines.
- Learn about security features and authentication in MongoDBin the following article, we will continue to introduce security and authentication features in the mongodb database management system series. in essence, the basic account validation feature is available in mongodb, but has been disabled in default mode, and in this mode the system requires at least 1 active database ...
- ObjectId in MongoDByou have seen the use of mongodb objectid in previous chapters. in this chapter, we will understand the structure of objectid.
- Text Search in MongoDBstarting with version 2.4, mongodb started supporting text indexes to search within the string content.
- Insert Document in MongoDBto insert data into collection in mongodb, you need to use the insert () or save () method.
- Auto-Increment Sequence in MongoDBmongodb does not have the auto-increment out-of-the-box feature like sql database. by default, it uses a 12-byte objectid for the _id field as the primary key to uniquely identify the documents. however, there are situations when we want the _id field to have some values that automatically increase beyond objectid.
- Shard in MongoDBsharding is a process of storing data records across multiple devices and it is a method of mongodb to meet the requirement for increasing data. when the size of the data increases, a single device cannot be enough to store data.
- Learn about Java Driver in MongoDBin the following article, we will introduce you some basic features of mongodv java driver as well as how to deploy and apply in practice.
- Install MongoDBinstructions for installing mongodb on windows.