![](/img/trans.png)
[英]Specify `max` Without `size` when Creating MongoDB Capped Collection
[英]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()
我得到了
錯誤的
你既需要size
和max
參數創建封頂集合:
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.