mongodb 根据月份删除mongodb中的旧记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18716461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:24:24  来源:igfitidea点击:

Remove old records in mongodb based on Month

mongodb

提问by Pawan

I am trying to delete Older records present in my collection .

我正在尝试删除我的收藏中存在的旧记录。

I have a collection named "user_track" , which consists of data in this format shown below

我有一个名为 "user_track" 的集合,它由如下所示的这种格式的数据组成

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

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

在我的集合 user_track 中,我只想保留当月的数据(即从 2013 年 9 月 1 日到当天),并想删除不属于当月的其余记录

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

Please suggest .

请建议。

回答by bsd

You can give any Date with Javascript date

您可以使用 Javascript 日期给出任何日期

db.user_track.remove( { access_time : {"$lt" : new Date(year, month_0_indexed, day)} })

So for removing documents before 1 September 2013 your command should be

因此,要在 2013 年 9 月 1 日之前删除文档,您的命令应该是

db.user_track.remove( { access_time : {"$lt" : new Date(2013, 8, 1) } })

September is the 9th month but the month field is zero indexed. So we make that as 8.

九月是第 9 个月,但月份字段的索引为零。所以我们把它设为 8。

回答by JAR.JAR.beans

Mongo has a TTL feature on collections, I think this is a nice solution to such cases:

Mongo 在集合上有一个 TTL 功能,我认为这是一个很好的解决方案:

https://docs.mongodb.com/manual/tutorial/expire-data/

https://docs.mongodb.com/manual/tutorial/expire-data/

Basically something like:

基本上是这样的:

db.log_events.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )

db.log_events.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )

回答by zero323

Probably there is a cleaner solution but this should work:

可能有一个更清洁的解决方案,但这应该有效:

  1. Create new Data field from date strings:

    var cursor = db.user_track.find()
    while (cursor.hasNext()) {
        var doc = cursor.next();
        db.user_track.update(
            {_id : doc._id},
            {$set : {access_time_ : new    Date(doc.access_time)}})
    }
    
  2. Now you can retrieve some records by comparing dates:

    db.user_track.find({access_time_: {$lt: new Date("Sep 01 2013 00:00:00 GMT+00:00")}})
    
  3. If everything works as expected remove obsolete records:

    db.user_track.remove({access_time_: {$lt: new Date("Sep 01 2013 00:00:00 GMT+00:00")}})
    
  4. In the future use date objects not strings

  1. 从日期字符串创建新的数据字段:

    var cursor = db.user_track.find()
    while (cursor.hasNext()) {
        var doc = cursor.next();
        db.user_track.update(
            {_id : doc._id},
            {$set : {access_time_ : new    Date(doc.access_time)}})
    }
    
  2. 现在您可以通过比较日期来检索一些记录:

    db.user_track.find({access_time_: {$lt: new Date("Sep 01 2013 00:00:00 GMT+00:00")}})
    
  3. 如果一切正常,请删除过时的记录:

    db.user_track.remove({access_time_: {$lt: new Date("Sep 01 2013 00:00:00 GMT+00:00")}})
    
  4. 将来使用日期对象而不是字符串

回答by Kamarey

In addition to other answers you may be interesting in the "Time to live” collection feature: http://docs.mongodb.org/manual/tutorial/expire-data/

除了其他答案,您可能会对“生存时间”集合功能感兴趣:http: //docs.mongodb.org/manual/tutorial/expire-data/

It's useful to automatically expire/remove documents from a collection after specific period of time.

在特定时间段后自动从集合中过期/删除文档很有用。

回答by Pawan

I have found a solution to address this issue .

我找到了解决此问题的解决方案。

var date=new Date();
date.setDate(date.getDate() - 1);
db.user_track.remove({"access_time":{"$lt":date}});

I will make this run automatically by putting these lines in a bash file and scheduling that script using a cron tab .

我将通过将这些行放在 bash 文件中并使用 cron tab 调度该脚本来自动运行。

Please share your views if this is a valid solution or not ??

如果这是一个有效的解决方案,请分享您的观点?