track deleted documents in Mongo DB's capped c

2019-07-21 14:56发布

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

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.

1条回答
叼着烟拽天下
2楼-- · 2019-07-21 14:59

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.

Delete old orders based on status

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).

Delete old/expired orders based on time

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 } )

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).

查看更多
登录 后发表回答