2

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.

silent_coder
  • 6,222
  • 14
  • 47
  • 91
  • 1
    Don't think this is duplicate. In question you mention not about capped collections, but about auto expires documents which have TTL, which is absolutelly different concept. So marking it duplicate is wrong – silent_coder Jul 13 '14 at 15:03

1 Answers1

3

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

Stennie
  • 63,885
  • 14
  • 149
  • 175
  • I need to use capped collection, because in other case delete operation will be pretty often occured (actually workflow is 1 put, 1 read, 1 delete) so if I will use basic collection I will faced with a problem of data fragmentaion in database, and then it will have a lot of negative sides for me. – silent_coder Jul 14 '14 at 07:58
  • 1
    @silent_coder: the space from deleted documents in a normal collection can be reused for future allocations. If you want to maximise space reuse you should enable the [`usePowerOf2Sizes` allocation strategy](http://docs.mongodb.org/manual/core/storage/#power-of-2-allocation) available in MongoDB 2.2+. This is now the default allocation strategy for collections created in MongoDB 2.6 (and automatically enabled in earlier versions for collections with TTL or Text indexes). – Stennie Jul 14 '14 at 09:25