Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following data.

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

And some lower dates, like 2014-3-9 , 8 ,7 ,6 etc.

When doing a db.coll.remove({date:{$lte:"2014-3-5"}})

Mongo removes the 15,14,13 aswell, but keeps single digit day dates. Is this maybe due to the date is a string?

I dont know how else to format the date so I can remove all dates below a certain date.

It is supposed to be a cleaning process, removing all documents with a date lower than specified.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.5k views
Welcome To Ask or Share your Answers For Others

1 Answer

Its because the date field you are querying on is a string filed and not a Date(). In your mongo documents instead of a custom date string, insert javascript date objects into date field.

like

{ deviceID: 186,,"date": new Date(2012, 7, 14) }

and when you execute the remove do it like

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...