masiboo
masiboo

Reputation: 4719

How to delete mongodb data which is less than a certain date

I have inserted few documents as

db.employees.insert({"_id": 7, "firstName": "neh","joining_date": "2017 03 24"})
db.employees.insert({"_id": 8, "firstName": "neh","joining_date": "2017 03 23"})
db.employees.insert({"_id": 9, "firstName": "neh","joining_date": "2017 03 22"})
db.employees.insert({"_id": 10, "firstName": "neh","joining_date": "2017 03 21"})

db.employees.find() show me:-

{ "_id" : 7, "firstName" : "neh", "joining_date" : "2017 03 24" }
{ "_id" : 8, "firstName" : "neh", "joining_date" : "2017 03 23" }
{ "_id" : 9, "firstName" : "neh", "joining_date" : "2017 03 22" }
{ "_id" : 10, "firstName" : "neh", "joining_date" : "2017 03 21" }

I wanted to delete documents less than date 2017 03 24. So I tried:

db.employees.remove( { joining_date : {$lt : new Date(2017, 3, 24) } });

it showed:-
WriteResult({ "nRemoved" : 0 })
Then tired:-

db.employees.remove( { joining_date : {$lt : new Date(2017, 2, 24) } }); 

Still it showed:-
WriteResult({ "nRemoved" : 0 })

Also tried:-

db.employees.remove( { joining_date : {$eq : new Date(2017, 3, 24) } });
db.employees.remove( { joining_date : {$eq : new Date(2017, 2, 24) } });  
db.employees.remove( { joining_date : {"$lt" : new Date(2017, 3, 24) } });
db.employees.remove( { joining_date : {"$lt" : new Date(2017, 2, 24) } });

All gives me:- WriteResult({ "nRemoved" : 0 })

A similar question was asked here But it doesn't work for me. What am I missing?

Upvotes: 0

Views: 1688

Answers (3)

prasad_
prasad_

Reputation: 14317

You can use any of the following two ways to delete documents from the collection, for a given "date" condition. The date comparison can be done either with the string date (it is possible in this case as date string is in "YYYY mm dd" format) or using a date object (this requires conversion of the string date to date object using the aggregation operator $dateFromString).

db.test.remove( { joining_date: { $gt: "2017 03 22"} } )

db.test.remove( { $expr: { $gt: [ { $dateFromString: { dateString: "$joining_date", format: "%Y %m %d" } }, ISODate("2017-03-22") ] } } )

Note the $dateFromString aggregation operator could be used in this query with the help of the $expr operator.

Upvotes: 2

Mohit Hurkat
Mohit Hurkat

Reputation: 416

db.employees.aggregate( [ {
   $project: {
      date: {
         $dateFromString: {
            dateString: '$joining_date',
                  format: "%Y %m %d",
            timezone: 'Asia/Kolkata'
         }
      }
   }
} ] ).forEach(function (doc){
    var query_date = new Date(2017, 2, 24)
    if(doc.date  < query_date){
              db.employees.remove({"_id": doc._id});
    }
});

This should work.

new Date() -> Month variable starts from 0(for January)

Upvotes: 0

Wernfried Domscheit
Wernfried Domscheit

Reputation: 59522

You compare a string ("2017 03 24") with a Date (new Date(2017, 3, 24)), this does not work.

Save joining_date as proper Date objects (the preferred way of doing it) or compare with string, e.g. {$eq: "2017 03 24"}

Upvotes: 0

Related Questions