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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:10:36+00:00 2026-05-25T17:10:36+00:00

I have a MongoDB collection containing history data with id and timestamp. I want

  • 0

I have a MongoDB collection containing history data with id and timestamp.

I want to delete data from the collection older than a specific
timestamp. But for every id at least one
document (the newest) must stay in the collection.

Suppose I have the following documents in my collection …

{"id" : "11", "timestamp" : ISODate("2011-09-09T10:27:34.785Z")} //1
{"id" : "11", "timestamp" : ISODate("2011-09-08T10:27:34.785Z")} //2

{"id" : "22", "timestamp" : ISODate("2011-09-05T10:27:34.785Z")} //3
{"id" : "22", "timestamp" : ISODate("2011-09-01T10:27:34.785Z")} //4

… and I want to delete documents having a timestamp older than
2011-09-07 then
1 and 2 should not be deleted because they are newer.
4 should be deleted because it is older, but 3 should not be deleted
(although it is older) because
at least one document per id should stay in the collection.

Does anyone know how I can do this with casbah and/or on the mongo
console?

Regards,
Christian

  • 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-25T17:10:37+00:00Added an answer on May 25, 2026 at 5:10 pm

    I can think of a couple of ways. First, try this:

    var cutoff = new ISODate("2011-09-07T00:00:00.000Z");
    db.testdata.find().forEach(function(data) {
        if (data.timestamp.valueOf() < cutoff.valueOf()) {
            // A candidate for deletion
            if (db.testdata.find({"id": data.id, "timestamp": { $gt: data.timestamp }}).count() > 0) {
                db.testdata.remove({"_id" : data._id});
             }
        }
    });
    

    This does the job you want. Or you can use a MapReduce job to do it as well. Load this into a text file:

    var map = function() {
        emit(this.id, {
            ref: this._id,
            timestamp: this.timestamp
        });
    };
    
    
    var reduce = function(key, values) {
        var cutoff = new ISODate("2011-09-07T00:00:00.000Z");
        var newest = null;
        var ref = null;
        var i;
        for (i = 0; i < values.length; ++i) {
            if (values[i].timestamp.valueOf() < cutoff.valueOf()) {
                // falls into the delete range
                if (ref == null) {
                    ref = values[i].ref;
                    newest = values[i].timestamp;
                } else if (values[i].timestamp.valueOf() > newest.valueOf()) {
                    // This one is newer than the one we are currently saving.
                    // delete ref
                    db.testdata.remove({_id : ref});
                    ref = values[i].ref;
                    newest = values[i].timestamp;
                } else {
                    // This one is older
                    // delete values[i].ref
                    db.testdata.remove({_id : values[i].ref});
                }
            } else if (ref == null) {
                ref = values[i].ref;
                newest = values[i].timestamp;
            }
        }
        return { ref: ref, timestamp: newest };
    };
    

    Load the above file into the shell: load("file.js");

    Then run it: db.testdata.mapReduce(map, reduce, {out: "results"});

    Then remove the mapReduce output: db.results.drop();

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

Sidebar

Related Questions

I have a MongoDB collection with data that was not saved through my Derby
I have an existing MongoDB collection containing user names. The user names contain both
Assuming I have a collection in MongoDB with 5000 records, each containing something similar
I have an applicaton written in PHP that retrieves files from mongoDB Grid collection,
I have a simple mongodb collection which stores tweets directly from the twitter api
We have a MongoDB collection containing nearly 40 million records. The current size of
I have MongoDB.BsonDocument, i want to convert that BsonDocument to List of collection, how
I have been tasked with porting data from a MongoDB database to a MySQL
I have a MongoDB collection Marks with fields student_id,Subject,Mark. I want to the average
Resulting from a MapReduce, I have a MongoDb collection that has the following structure:

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.