[英]MongoDB doesn't respect max for capped collection
Here's how I'm creating the 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)
I set self.max_size
to 10
.我将
self.max_size
设置为10
。
Then, I tried this:然后,我尝试了这个:
for i in range(20):
cache.put(i, {'id': i})
I expected to see exactly 10
documents in the collection, but in reality all the 20
documents are in the collection.我希望在集合中正好看到
10
文档,但实际上所有20
文档都在集合中。
Why?为什么?
Update: I ran the following in mongo shell:更新:我在 mongo shell 中运行了以下命令:
db.getCollection('test').isCapped()
And I got我得到了
false
错误的
You need both size
and max
parameters to create the capped collection:你既需要
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})
In the mongo
shell:在
mongo
外壳中:
> db.test.find()
{ "_id" : 5 }
{ "_id" : 6 }
{ "_id" : 7 }
{ "_id" : 8 }
{ "_id" : 9 }
> db.test.isCapped()
true
If you try to create it without the size
parameter, it will output an error:如果尝试在没有
size
参数的情况下创建它,则会输出错误:
pymongo.errors.OperationFailure: the 'size' field is required when 'capped' is true
However your code have a try...except
parameter.但是你的代码有一个
try...except
参数。 So it hit this error, then proceeded to the except
clause, so you didn't see that an error was produced and the capped collection was not created.所以它遇到了这个错误,然后继续执行
except
子句,所以你没有看到产生了错误并且没有创建上限集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.