mongodb remove all dates less than specified

前端 未结 4 1147
孤独总比滥情好
孤独总比滥情好 2021-01-02 06:50

I have the following data.

{
    deviceID: 186,
    date: \"2014-3-15\"
}
{
    deviceID: 186,
    date: \"2014-3-14\"
}
{
    deviceID: 186,
    date: \"201         


        
4条回答
  •  太阳男子
    2021-01-02 07:36

    This is because you are storing your data in a wrong format. You have a string an string '15' is smaller than string '5'. Convert your strings in the beginning to date (read here how to use dates in mongo).

    And only than you can use it to properly compare your dates:

    db.coll.remove({
      date:{
        $lte : new Date(2012, 7, 14)
      }
    })
    

提交回复
热议问题