在MongoDB中的集合中对记录进行分区 [英] Partitioning records in a collection in MongoDB

查看:496
本文介绍了在MongoDB中的集合中对记录进行分区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用例,需要在指定的时间间隔后删除集合中的一组记录. 例如:超过10小时的记录将每10小时删除一次.

I have an usecase where a set of records in a collection need to be deleted after a specified interval of time. For ex: Records older than 10hours be deleted every 10th hour.

我们曾尝试根据ID进行删除,但发现删除速度很慢.

We have tried deletion based on id but found it to be slow.

是否有一种方法可以按需要在Mongo中对集合中的记录进行分区并删除分区

Is there a way to partition the records in a collection and drop a partition as and when required in Mongo

推荐答案

MongoDB当前不支持分区,有一张JIRA票证将此功能添加为功能(

MongoDB does not currently support partitions, there is a JIRA ticket to add this as a feature (SERVER-2097).

一种解决方案是利用多个基于时间的集合,以与分区类似的方式循环集合.通常,当您通常只查询这些基于时间的集合中的一个或几个时,我们会这样做.如果您经常需要阅读多个集合,则可以添加一些包装器代码以简化该过程.

One solution is to leverage multiple, time-based collections, cycling collections in a similar way as you would partitions. Typically we would do this when you'd usually only be querying one or few of these time-based collections. If you would often need to read across multiple collections, you could add some wrapper code to simplify that.

还有 TTL索引,该索引利用了mongod服务器为您处理删除操作.

There's also TTL Indexes, which leverage a background thread in the mongod server to handle the deletes for you.

由于多种原因,您通过_id进行的删除操作可能很慢,并且可能需要对您的原始问题进行更多的调查.

Your deletes by _id may have been slow for a number of reasons, and probably warrants more investigation beyond your original question.

这篇关于在MongoDB中的集合中对记录进行分区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆