簡體   English   中英

MongoDB 不尊重上限集合的最大值

[英]MongoDB doesn't respect max for capped collection

這是我創建集合的方式:

def connect(self):
    self.client = MongoClient(self.ip, self.port)
    try:
        capped = self.max_size != None
        print("capped", capped)
        print("max", self.max_size)
        self.coll = self.client[self.db_name].create_collection(self.coll_name, capped=capped, max=self.max_size)
    except:
        self.coll = self.client[self.db_name][self.coll_name]
    self.coll.create_index([('expireAt', 1)], expireAfterSeconds=0)

我將self.max_size設置為10

然后,我嘗試了這個:

for i in range(20):
    cache.put(i, {'id': i})

我希望在集合中正好看到10文檔,但實際上所有20文檔都在集合中。

為什么?

更新:我在 mongo shell 中運行了以下命令:

db.getCollection('test').isCapped()

我得到了

錯誤的

需要sizemax參數創建封頂集合:

conn.test.create_collection('test', capped=True, size=10000, max=5)

for i in range(10):
    conn.test.test.insert_one({'_id': i})

mongo外殼中:

> db.test.find()
{ "_id" : 5 }
{ "_id" : 6 }
{ "_id" : 7 }
{ "_id" : 8 }
{ "_id" : 9 }

> db.test.isCapped()
true

如果嘗試在沒有size參數的情況下創建它,則會輸出錯誤:

pymongo.errors.OperationFailure: the 'size' field is required when 'capped' is true

但是你的代碼有一個try...except參數。 所以它遇到了這個錯誤,然后繼續執行except子句,所以你沒有看到產生了錯誤並且沒有創建上限集合。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM