有没有一种方法来限制某些集合中的记录数 [英] Is there a way to limit the number of records in certain collection
问题描述
假设我插入以下记录(例如foo1,foo2,foo3,foo4,.... foo10) 我希望集合在任何时间点仅保留5条记录(例如,可能是foo1,...,foo5或foo2,... foo6或foo6,... foo10)
Assuming I insert the following records (e.g. foo1, foo2, foo3, foo4, .... foo10) I would like the collection to retain only 5 records at any point in time (e.g. it could be foo1, ... foo5 OR foo2, ... foo6 or foo6, ... foo10)
我应该如何实现?
推荐答案
听起来像您在寻找上限收藏:
上限集合是固定大小的集合...
[...]
一旦空间得到充分利用,新添加的对象将替换集合中最旧的对象.
Capped collections are fixed sized collections...
[...]
Once the space is fully utilized, newly added objects will replace the oldest objects in the collection.
您可以使用类似于
You can achieve this using a command similar to
db.createCollection("collectionName",{capped:true,size:10000,max:5})
其中10000是字节大小,5是您将限制在集合中的最大文档数.
where 10000 is the size in bytes and 5 is the maximum number of documents you would restrict in the collection.
这篇关于有没有一种方法来限制某些集合中的记录数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!