根据月份删除mongodb中的旧记录 [英] Remove old records in mongodb based on Month
问题描述
我正在尝试删除收藏夹中存在的旧记录.
I am trying to delete Older records present in my collection .
我有一个名为"user_track"的集合,其中包含以下所示格式的数据
I have a collection named "user_track" , which consists of data in this format shown below
db.user_track.find().pretty()
{
"_id" : ObjectId("50c9afe5765fb0e4fea076ce"),
"cust_id" : "ddddd",
"symbol" : "WWWW",
"access_time" : "Thu Dec 13 2012 05:37:25 GMT-0500 (EST)"
}
{
"_id" : ObjectId("50c9afe7765fb0e4ffa076ce"),
"cust_id" : "eeeeeeeeee",
"symbol" : "WFC",
"access_time" : "Thu Dec 13 2012 05:37:27 GMT-0500 (EST)"
}
{
"_id" : ObjectId("522de3ae2191b0e41a7534dd"),
"cust_id" : "dsdsds",
"symbol" : "APPLE",
"access_time" : "Mon Sep 09 2013 11:05:18 GMT-0400 (EDT)"
}
在我的收藏集user_track中,我只想保留当月的数据(即从2013年9月1日到当日),并希望删除不属于当月的其余记录
In my collection , user_track , i want to keep only the current month's data (that is from Sep 01 2013 to current day ) and want to delete the rest of the records which doesn't belong to current month
我试图发出以下命令,但是由于日期格式不同,所以我不确定如何发出此命令来满足我的要求.
I have tried to issue the below command , but i am not sure of how to issue this command to suit my requirement as the date i have is in different format .
db.user_track.delete( { 'access_time': {$lte: XXX} })
请提出建议.
推荐答案
您可以使用Javascript日期指定任何日期
You can give any Date with Javascript date
db.user_track.remove( { access_time : {"$lt" : new Date(year, month_0_indexed, day)} })
因此,要在2013年9月1日之前删除文档,您的命令应为
So for removing documents before 1 September 2013 your command should be
db.user_track.remove( { access_time : {"$lt" : new Date(2013, 8, 1) } })
9月是第9个月,但month字段的索引为零.所以我们将其设为8.
September is the 9th month but the month field is zero indexed. So we make that as 8.
这篇关于根据月份删除mongodb中的旧记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!