Mongo DB remove old data using remove command

给你一囗甜甜゛ 提交于 2019-12-13 04:37:39

问题


My mongo db contains

> db.collection.find();
{ "_id" : ObjectId("55f8e493d97731874b1235bd"), "Date" : "14-12-2014", "Value" : 25.36 }
{ "_id" : ObjectId("55f8e497d97731874b1235be"), "Date" : "14-11-2014", "Value" : 25.36 }
{ "_id" : ObjectId("55f917f748f91b35cf1ddaac"), "Date" : "14-01-2015", "Value" : 25.36 }
{ "_id" : ObjectId("55f917fc48f91b35cf1ddaad"), "Date" : "14-02-2015", "Value" : 25.36 }
{ "_id" : ObjectId("55f9180148f91b35cf1ddaae"), "Date" : "14-03-2015", "Value" : 25.36 }
{ "_id" : ObjectId("55f9180848f91b35cf1ddaaf"), "Date" : "14-04-2015", "Value" : 25.36 }
{ "_id" : ObjectId("55f9180f48f91b35cf1ddab0"), "Date" : "14-05-2015", "Value" : 25.36 }
{ "_id" : ObjectId("55f9181448f91b35cf1ddab1"), "Date" : "14-06-2015", "Value" : 25.36 }
{ "_id" : ObjectId("55f9181948f91b35cf1ddab2"), "Date" : "14-07-2015", "Value" : 25.36 }
{ "_id" : ObjectId("55f9181d48f91b35cf1ddab3"), "Date" : "14-08-2015", "Value" : 25.36 }

I want to delete three months old data. For that

> db.collection.remove( { "Date": {$lt:"14-10-2014"} } );

I tried like this but it's not working properly. It's deleting depending only on month e.g. it's deleting from today to jan 2105 and ninth month of 2014 to jan 2104 but not December & November of 2104. How do I solve this?


回答1:


I think you can try the below code. You can remove the docs without updating the Date field to ISODate. As a good practice, you should always store date as an ISODate. It makes querying easy.

var cursor = db.collection.find()
while (cursor.hasNext()) {
 var doc = cursor.next();
 var docDate = doc.Date;
 var queryDate = "14-10-2014";
 var docDateMillis = new Date(docDate.substring(6,10), docDate.substring(3,5)-1, docDate.substring(0,2)).getTime();
 var queryDateMillis = new Date(queryDate.substring(6,10), queryDate.substring(3,5)-1, queryDate.substring(0,2)).getTime()
 if(docDateMillis < queryDateMillis) {
   db.collection.remove({_id : doc._id})
 }     
}



回答2:


Your Date field is not valid so first you need to update your documents. The best way is using the "Bulk()" API

var bulk = db.collection.initializeOrderedBulkOp(),   
    count = 0

db.collection.find().forEach(function(doc) { 
    var newDate = new Date(doc.Date.replace(/(\d{2})-(\d{2})/, "$2-$1")); 
    bulk.find({ "_id": doc._id }).updateOne({ 
        "$set": { "Date": newDate }});
    count++;
    if (count % 100 == 0) {
        bulk.execute();
        bulk = db.collection.initializeUnorderedBulkOp();
    }
})

if (count % 100 != 0)
    bulk.execute();

Your documents now look like this:

{ "_id" : ObjectId("55f8e493d97731874b1235bd"), "Date" : ISODate("2014-12-13T21:00:00Z"), "Value" : 25.36 }
{ "_id" : ObjectId("55f8e497d97731874b1235be"), "Date" : ISODate("2014-11-13T21:00:00Z"), "Value" : 25.36 }
{ "_id" : ObjectId("55f917f748f91b35cf1ddaac"), "Date" : ISODate("2015-01-13T21:00:00Z"), "Value" : 25.36 }
{ "_id" : ObjectId("55f917fc48f91b35cf1ddaad"), "Date" : ISODate("2015-02-13T21:00:00Z"), "Value" : 25.36 }
{ "_id" : ObjectId("55f9180148f91b35cf1ddaae"), "Date" : ISODate("2015-03-13T21:00:00Z"), "Value" : 25.36 }
{ "_id" : ObjectId("55f9180848f91b35cf1ddaaf"), "Date" : ISODate("2015-04-13T21:00:00Z"), "Value" : 25.36 }
{ "_id" : ObjectId("55f9180f48f91b35cf1ddab0"), "Date" : ISODate("2015-05-13T21:00:00Z"), "Value" : 25.36 }

Then you can use the .remove method to remove old document.

db.collection.remove({ "Date": { "$lt": new Date("10-14-2014")} }



回答3:


This would do the job

    db.test.remove({'$where':function(){ 
        var x = this.Date.split('-');
        var dt = new Date(x[2], x[1] - 1, x[0]);
        var y='14-10-2014'.split('-');
        var chkDt = new Date(y[2], y[1] - 1, y[0]);
        return dt < chkDt; }})

Although it would be better if you use ISODate() in the document while storing



来源:https://stackoverflow.com/questions/32602220/mongo-db-remove-old-data-using-remove-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!