在Mongo DB的上限集合中跟踪已删除的文档 [英] track deleted documents in Mongo DB's capped collection

查看:81
本文介绍了在Mongo DB的上限集合中跟踪已删除的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何选项可以跟踪从有上限的收藏夹中删除文档的时间?

Is there any options which allow to track document when it goes to be deleted from capped collection?

例如,我有一个状态为-处理与否的订单清单.因此,当mongo决定删除某些文档时,我想每次将订单保留在有上限的集合中并检查状态.如果订单已处理-很酷,就让他离开,但如果订单没有处理,则将其推回队列.

For example, I have a list of orders with the status - handled or not. So I want to persist that orders in in capped collection and check status each time, when mongo decide to delete some doc. If order was handled - so, cool, just let him leave, but if it wasn't handled, just push it back to the queue.

推荐答案

上限​​集合是固定大小的集合,一旦集合已满,它们就会通过覆盖最旧的文档来自动删除它们(根据插入顺序).

Capped collections are fixed-sized collections which automatically remove the oldest documents (based on insertion order) by overwriting them once the collection is full.

从概念上讲,有上限的集合是循环缓冲区,而不是队列.

Conceptually capped collections are a circular buffer rather than a queue.

这听起来不太适合用于保留订单信息的用例-删除文档时,如果您的上限集合太小,则没有钩子"可以将文档重新插入上限集合中您可以在处理文档之前将其覆盖.

This doesn't sound like a good fit for a use case of persisting order information -- there is no "hook" to re-insert documents into a capped collection when they are deleted, and if your capped collection is too small you may overwrite documents before they are processed.

我认为您真的希望使用普通的(无上限)收藏.

I think you really want to be using a normal (non-capped) collection.

如果要基于某个状态值删除订单,则应在应用程序代码中处理此问题(例如,当订单从待处理"状态变为已处理"状态时,请执行任何清理操作).

If you want to delete orders based on some state value, you should handle this in your application code (for example, when a order moves from "Pending" to "Processed" state do whatever cleanup is required).

如果您希望在特定时间自动删除旧订单/过期订单,那么一种好的方法是使用具有

If you want to have old/expired orders automatically deleted at a specific time, a good approach would be using a normal collection with a document-level Time-To-Live (TTL) expiry, eg:

db.orders.ensureIndex( { "expireAt": 1 }, { expireAfterSeconds: 0 } )

然后您可以根据expireAt日期为每个订单单据设置(或延长)有效期.请注意,如果您需要同时检查expireAt值和状态字段(TTL到期仅基于所提供的日期索引),则此选项可能不合适.

You could then set (or extend) the expiry for each order document based on the expireAt date. Note that this may not be a suitable option if you need to check both the expireAt value and a status field (TTL expiry is solely based on the date index provided).

这篇关于在Mongo DB的上限集合中跟踪已删除的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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