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 7834747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:18:10+00:00 2026-06-02T13:18:10+00:00

In my current project we use Mongo for storing a lot of documents (approximately

  • 0

In my current project we use Mongo for storing a lot of documents (approximately 100Bln).
How do I remove a half of oldest documents using field _id, because if I use indexed field “timestamp” this operation will be completed after ~3 years with current speed.

  • 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-06-02T13:18:12+00:00Added an answer on June 2, 2026 at 1:18 pm

    Here is a link to a MongoDB-User Google Groups post that discusses generating ObjectIds based on time stamps:
    http://groups.google.com/group/mongodb-user/browse_thread/thread/262223bb0bd52a83/3fd9b01d0ad2c41b

    From the post:
    Extracting the time stamp from Mongo ObjectIds is explained in the
    Mongo Document “Optimizing Object IDs”
    http://www.mongodb.org/display/DOCS/Optimizing+Object+IDs#OptimizingObjectIDs-Extractinsertiontimesfromidratherthanhavingaseparatetimestampfield.

    Taken from the example in the post, ObjectIds may be created from the time in seconds in Unix time:

    > now = new Date()
    ISODate("2012-04-19T19:01:58.841Z")
    > ms = now.getTime()
    1334862118841
    > sec = Math.floor(ms/1000)
    1334862118
    > hex = sec.toString(16)
    4f906126
    > id_string = hex + "0000000000000000"
    4f9061260000000000000000
    > my_id = ObjectId(id_string)
    ObjectId("4f9061260000000000000000")
    

    Using the above formula, you can create an ObjectID from any date, and query for documents with lesser ObjectIds.

    Going forward, if your application will be saving data based on time and deleting data once it reaches a certain age, you may find it preferable to store your documents in separate collections; one for each day, week, or whatever time frame makes the most sense for your application. Dropping an entire collection requires a lot less overhead than removing individual documents, because it can be done with a single operation. db.<collection>.remove({query}) will perform a write operation for each document returned, which as you have observed may be prohibitively slow for a large number of documents.

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

Sidebar

Related Questions

I usually use Nunit but in my current project I am using MSTest. Now
Our current project does not use Hibernate (for various reasons) and we are using
im using t4mvc in my current project and am trying to use the routing
I generally use StructureMap, but on a current project I'm using Castle Windsor (2.1).
In my current project, we use lots of user controls. Large user controls (500+
I use both Spring and Spock plugins in my current project. I wrote a
I am looking to use an IOC container in my current project which is
I'm making pretty heavy use of reflection in my current project to greatly simplify
I am interested in applying dependency injection to my current project, which makes use
I'm trying to convert a current Django project in development to use zc.buildout So

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.