Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7639785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:30:36+00:00 2026-05-31T08:30:36+00:00

I want to use a capped collection as a cache store, I plan on

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:30:38+00:00Added an answer on May 31, 2026 at 8:30 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use a capped collection in Mongo, but I don't want my
I need to use HTML comments to store specific data, but I don't want
I am using jruby, i want use pagination for my rails app i used
I want use MySQL database in R environment. I am using Ubuntu OS. To
I want use groovy findAll with my param to filtering closure filterClosure = {
i want use some data from a website with web service. i have a
I have a transaction log file in CSV format that I want use to
Below is my stored procedure. I want use stored procedure select all row of
I want to use the mouse scrollwheel in my OpenGL GLUT program to zoom
I want to use Powershell to write some utilities, leveraging our own .NET components

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.