Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

mongodb remove all dates less than specified

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)}})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...