在mongodb中限制集合中的文档数 [英] Limit the number of documents in a collection in mongodb
问题描述
我有一个用作缓存的集合.如何在此馆藏中将文件数限制为5M. 注意:可以更新的缓存项将存在于缓存中. 我曾经尝试使用上限,但是保存在上限集合中的项目无法修改.
I have a collection that use as a cache. How can I limit the documents number to 5M inside this collection. Note: that the cache item can be updated will existing in the cache. I use tried to use capped, but the item saves in a capped collection cannot be modified.
有没有办法做到这一点? (我想避免删除该项,然后插入更新项)
Is there a way to do that? (i would like to avoid delete the item and then insert the update one)
推荐答案
不幸的是,您在这里没有什么可以做的.如您所述,无法修改上限集合中的文档.而且,您不能在文档中指定限制,以IIRC为单位,以兆字节为单位.
Unfortunately, there's not much you can do here. As you noted, documents in capped collections can't be modified. And also, you can't specify a limit in documents, it's in megabytes, IIRC.
因此,最好的选择是使用常规收集并定期进行工作,以检查收集是否溢出并清理旧文档.
So, your best bet is to use normal collection and have a periodic job that'll check the collection for overflows and will clean up old documents.
这篇关于在mongodb中限制集合中的文档数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!