自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何把MongoDB作為循環(huán)隊(duì)列

運(yùn)維 數(shù)據(jù)庫運(yùn)維 MongoDB
我們在使用MongoDB的時(shí)候,一個集合里面能放多少數(shù)據(jù),一般取決于硬盤大小,只要硬盤足夠大,那么我們可以無休止地往里面添加數(shù)據(jù)。

 [[387464]]

我們在使用MongoDB的時(shí)候,一個集合里面能放多少數(shù)據(jù),一般取決于硬盤大小,只要硬盤足夠大,那么我們可以無休止地往里面添加數(shù)據(jù)。

然后,有些時(shí)候,我只想把MongoDB作為一個循環(huán)隊(duì)列來使用,期望它有這樣一個行為:

  1. 設(shè)定隊(duì)列的長度為10
  2. 插入第1條數(shù)據(jù),它被放在第1個位置
  3. 插入第2條數(shù)據(jù),它被放在第2個位置
  4. ...
  5. 插入第10條數(shù)據(jù),它被放在第10個位置
  6. 插入第11條數(shù)據(jù),它被放在第1個位置,覆蓋原來的內(nèi)容
  7. 插入第12條數(shù)據(jù),它被放在第2個位置,覆蓋原來的內(nèi)容
  8. ...

MongoDB有一種Collection叫做capped collection,就是為了實(shí)現(xiàn)這個目的而設(shè)計(jì)的。

普通的Collection不需要提前創(chuàng)建,只要往MongoDB里面插入數(shù)據(jù),MongoDB自動就會創(chuàng)建。而capped collection需要提前定義一個集合為capped類型。

語法如下:

  1. import pymongo 
  2.  
  3. conn = pymongo.MongoClient() 
  4. db = conn.test_capped 
  5.  
  6. db.create_collection('info', capped=Truesize=1024 * 1024 * 10, max=5) 

對一個數(shù)據(jù)庫對象使用create_collection方法,創(chuàng)建集合,其中參數(shù)capped=True說明這是一個capped collection,并限定它的大小為10MB,這里的size參數(shù)的單位是byte,所以10MB就是1024 * 1024 * 10. max=5表示這個集合最多只有5條數(shù)據(jù),一旦超過5條,就會從頭開始覆蓋。

創(chuàng)建好以后,capped collection的插入操作和查詢操作就和普通的集合完全一樣了:

  1. col = db.info 
  2. for i in range(5): 
  3.     data = {'index': i, 'name''test'
  4.     col.insert_one(data) 

這里我插入了5條數(shù)據(jù),效果如下圖所示:

其中,index為0的這一條是最先插入的。

接下來,我再插入一條數(shù)據(jù):

  1. data = {'index': 100, 'name''xxx'
  2. col.insert_one(data) 

此時(shí)數(shù)據(jù)庫如下圖所示:

可以看到,index為0的數(shù)據(jù)已經(jīng)被最新的數(shù)據(jù)覆蓋了。

我們再插入一條數(shù)據(jù)看看:

  1. data = {'index': 999, 'name''xxx'
  2. col.insert_one(data) 

運(yùn)行效果如下圖所示:

可以看到,index為1的數(shù)據(jù)也被覆蓋了。

這樣我們就實(shí)現(xiàn)了一個循環(huán)隊(duì)列。

MongoDB對capped collection有特別的優(yōu)化,所以它的讀寫速度比普通的集合快。

但是capped collection也有一些缺點(diǎn),在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.

意思就是說,capped collection里面的每一條記錄,可以更新,但是更新不能改變記錄的大小,否則更新就會失敗。

不能單獨(dú)刪除capped collection中任何一條記錄,只能整體刪除整個集合然后重建。

本文轉(zhuǎn)載自微信公眾號「未聞Code」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系未聞Code公眾號。

 

責(zé)任編輯:武曉燕 來源: 未聞Code
相關(guān)推薦

2022-04-29 08:48:25

開源

2009-04-17 16:16:53

程序人生職場

2018-03-29 08:38:10

2021-04-23 13:20:13

Redis數(shù)據(jù)庫代碼

2020-12-17 09:38:16

設(shè)計(jì)模式參數(shù)

2013-09-22 13:25:54

MongoDB內(nèi)存數(shù)據(jù)庫

2010-03-11 14:15:24

Python循環(huán)

2023-09-05 15:48:14

RabbitMQ延遲隊(duì)列

2011-08-24 16:56:54

OracleArray類型存儲過程

2017-05-02 22:38:44

前端開發(fā)JS事件循環(huán)機(jī)制

2009-12-14 10:32:26

Ruby Gnome

2024-03-22 12:10:39

Redis消息隊(duì)列數(shù)據(jù)庫

2010-04-21 14:39:59

Unix消息隊(duì)列

2025-01-15 07:54:02

2024-05-10 14:46:27

Pythonfor循環(huán)

2021-07-21 14:29:27

Python編程語言軟件開發(fā)

2019-04-15 14:40:46

消息隊(duì)列Java編程

2024-12-06 16:00:00

C++頭文件

2011-06-03 10:06:57

MongoDB

2019-10-15 14:53:23

MongoDBMySQL數(shù)據(jù)庫
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號