When we use MongoDB, the amount of data we can fit into a collection generally depends on the size of the hard drive, and as long as the hard drive is large enough, we can add data to it endlessly.

Then, there are times when I just want to use MongoDB as a circular queue and expect it to behave like this:

  1. Set the queue length to 10
  2. Insert the first piece of data, it is placed in the first position
  3. Insert the second piece of data, which is placed in the second position
  4. .
  5. Insert the 10th piece of data, which is placed in the 10th position
  6. Insert the eleventh piece of data, which is placed in the first place, overwriting the original content
  7. Insert the 12th piece of data, which is placed in the second place, overwriting the original content
  8. .

MongoDB has a capped Collection called a capped Collection that is designed for this purpose.

Normal collections do not need to be created in advance, as long as you insert data into MongoDB, MongoDB will automatically create. Capped Collection requires a capped collection to be defined in advance as a capped type.

The syntax is as follows:

import pymongo

conn = pymongo.MongoClient()
db = conn.test_capped

db.create_collection('info', capped=True, size=1024 * 1024 * 10, max=5)
Copy the code

Use the create_collection method on a database object to create a capped collection with the parameter capped=True stating that it is a capped collection and that its size is capped at 10MB, where the size parameter is in bytes, So 10MB is 1024 * 1024 * 10. Max =5 means that the set has a maximum of 5 pieces of data, and once more, it will be overwritten from scratch.

Once created, the capped Collection’s insert and query operations are exactly the same as normal collections:

col = db.info
for i in range(5):
    data = {'index': i, 'name': 'test'}
    col.insert_one(data)
Copy the code

Here I inserted 5 pieces of data, which looks like the following:

The one whose index is 0 is inserted first.

Next, I insert another piece of data:

data = {'index': 100.'name': 'xxx'}
col.insert_one(data)
Copy the code

The database is as follows:

As you can see, the index 0 has been overwritten by the latest data.

Let’s insert another piece of data:

data = {'index': 999.'name': 'xxx'}
col.insert_one(data)
Copy the code

The operating effect is shown in the figure below:

As you can see, the data with index 1 is also overwritten.

So we have a circular queue.

MongoDB has special optimization for capped Collection so it can read and write faster than normal collections.

Capped Collection has some disadvantages, however, as mentioned in the official MongoDB documentation:

If an update or a replacement operation changes the document size, the operation will fail.

You cannot delete documents from a capped collection. To remove all documents from a collection, use the drop() method to drop the collection and recreate the capped collection.

This means that each record in a capped collection can be updated, but updates cannot change the record size, otherwise the update will fail.

You cannot individually delete any record in a capped Collection; you can only delete the entire collection and rebuild it.

If this article is helpful to you, please follow my wechat official account: WEIwei Code(ID: itskingName)