In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to take MongoDB as a circular queue". In daily operation, I believe that many people have doubts about how to use MongoDB as a circular queue. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to treat MongoDB as a circular queue". Next, please follow the editor to study!
When we use MongoDB, how much data can be put in a collection generally depends on the size of the hard disk, as long as the hard disk is big enough, then we can add data to it endlessly.
Then, sometimes, I just want to use MongoDB as a circular queue, expecting it to behave like this:
Set the queue length to 10
Insert the first piece of data, which is placed in the first position
Insert the second piece of data, which is placed in the second position
...
Insert the 10th data, which is placed in the 10th position
Insert item 11 data, which is placed in the first place, overwriting the original content
Insert item 12 data, which is placed in the second position, overwriting the original content
...
MongoDB has a Collection called capped collection, which is designed to achieve this purpose.
A normal Collection does not need to be created in advance, as long as you insert data into the MongoDB, the MongoDB will be created automatically. Capped collection needs to define a collection as a capped type in advance.
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)
Use the create_collection method on a database object to create a collection, where the parameter capped=True indicates that this is a capped collection and limits its size to 10MB. Here, the unit of the size parameter is byte, so 10MB is 1024 * 1024 * 10. Max=5 means that the collection has at most 5 pieces of data. Once it exceeds 5, it will be overwritten from scratch.
Once created, the insert and query operations of capped collection are exactly the same as normal collections:
Col = db.info for i in range (5): data = {'index': I,' name': 'test'} col.insert_one (data)
Here I have inserted 5 pieces of data, and the effect is as follows:
Among them, the one with an index of 0 is inserted first.
Next, I insert another piece of data:
Data = {'index': 100,' name':' xxx'} col.insert_one (data)
The database is shown in the following figure:
As you can see, the data with an index of 0 has been overwritten by the latest data.
Let's insert another piece of data to see:
Data = {'index': 999,' name': 'xxx'} col.insert_one (data)
The running effect is shown in the following figure:
As you can see, the data with an index of 1 is also overwritten.
In this way, we implement a circular queue.
MongoDB has a special optimization for capped collection, so it reads and writes faster than normal collections.
But capped collection also has some disadvantages, as mentioned in the official documentation of MongoDB:
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.
It means that every record in capped collection can be updated, but the update cannot change the size of the record, otherwise the update will fail.
You cannot delete any single record in the capped collection alone, you can only delete the entire collection and rebuild it.
At this point, the study on "how to use MongoDB as a circular queue" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.