I want to use a capped collection as a cache store, I plan on selecting using a compound index – key and expiry-date. Since it’s impossible to update/delete from a capped collection, I will add new entries with new expiry dates and just select the one with future expiry.
1) Is this the optimal way of creating the index if I’ll be using Query.GTE("expiry", DateTime.Now) in the query?
cacheColl.EnsureIndex(new IndexKeysBuilder().Ascending("key").Descending("expiry"));
2) Do I need a [BsonId] attribute on the class? I know that “key” won’t be unique. Does a record need to have a unique id entry??
3) My only motivation for using a capped collection is to limit the final size of the cache (both disk and memory) and not having to delete expired cache items myself. Is there a reason to prefer a regular collection and update items / delete expired ones? Even if I delete the documents, I read that space is not freed (would I need to compact?)
1) The index looks about right. You can also add a sort descending and limit 1 to the query if you only care about the latest.
2) No. In a capped collection, _id is not automatically created and is not required. The reason why I needs to be unique on normal collections is because a unique index on _id is created for that collection by default.
3) There are pros and cons to both approach and which is better is totally dependent on your needs. One thing you might want to consider about capped collection is that it will not be easy to resize the collection once you created it. This will be problematic if you realize later on that the size you initially set was too small to fit into the time frame window that you needed.
P.S. You are right about the part that the space used by extents of a deleted document is not being freed back. However, Mongo keeps track of these extents and reuse them whenever possible.