简体   繁体   English

当某个日期小于新的Date()时,如何从mongodb集合中删除文档

[英]How to delete a document from a mongodb collection when some date is smaller than new Date()

I'm trying to delete (while importing new documents) some documents from my MongoDB collection, when some date in the document is smaller than new Date() . 当文档中的某个date小于new Date()时,我试图从MongoDB集合中删除(导入新文档时)一些文档。

Everytime I import a document, I check, if there is a document with the same STATION_ID that needs to be deleted. 每次导入文档时,我都会检查是否需要删除具有相同STATION_ID的文档。

Now I'm doing it like this using mongoose: 现在,我使用猫鼬这样做:

function doTheStuff(stationid, time, callback) {
  OwnMetarDoc.find({
    STATION_ID: stationid[0]
  }, function(err, entry) {
    var isTaf = false;
    if (entry[0] != undefined) {
      if (entry[0].TIME) {
        var entryObjectTimerec = entry[0].TIME;
        var isTaf = true;
      };
      var d = new Date;
      var month = d.getUTCMonth();
      var year = d.getUTCFullYear();
      var timeRecConverted = new Date(year, month, parseInt(entryObjectTimerec.substring(0, 2)),
          parseInt(entryObjectTimerec.substring(2, 4)), parseInt(entryObjectTimerec.substring(4, 6)));
      //The relevant part
      if (timeRecConverted.getTime() < new Date() && isTaf == true) {
        OwnMetarDoc.remove({
          _id: entry[0]._id
        }, function(err, result) {
          if (err) {
            console.log(err);
          } else {
            console.log('old taf entry removed');
          };
        });
      }
      if (typeof callback === 'function') {
          callback()
      };
    };
  })
};

But this takes way too long when i have over 100'000 entries and multiple callbacks. 但是当我有超过100'000个条目和多个回调时,这花费了太长时间。

Does anybody knows if there is a way to achieve this quickly and automatically? 有人知道是否有办法快速,自动地做到这一点吗?

I know how to do it when I have to delete a document that is older than fe eight hours within the Schema like this: 我必须删除在架构中这样早于fe八个小时的文档时才知道该怎么做:

var ownMetarSchema = new Schema({
  STATION_ID: String,
  TIMERECASDATE: {
    type: Date,
    expires: 28800
  }
})

But I don't know how to do it with my previous example. 但是我不知道如何使用上一个示例。

ps: I'm relatively new to Javascript and you may need some more information, just ask me please. ps:我对Java语言还比较陌生,您可能需要更多信息,请问我一下。

If I understand, you want to update your document. 据我了解,您想更新您的文档。 You should read the mongo docs. 您应该阅读mongo文档。 Here : http://docs.mongodb.org/manual/tutorial/modify-documents/ You can just replace all value with your new value. 此处: http : //docs.mongodb.org/manual/tutorial/modify-documents/您可以将所有值替换为新值。 Hope that helps. 希望能有所帮助。

I would assume that you would have used the TTL part, if you know how long your weather forecasts would be. 如果您知道天气预报会持续多久,我会假设您将使用TTL部分。 Since you haven't used TTL , that means your forecast could be one day in the future or even one month (just for the discussion). 由于您尚未使用TTL ,这意味着您的预测可能是将来的一天甚至一个月(仅用于讨论)。

You are finding all the documents with STATION_ID and the parsing through it. 您将找到所有带有STATION_ID的文档并进行解析。 Instead, you can do the following: 相反,您可以执行以下操作:

If you have a field date with the exact timestamp, then just run 如果您具有带有确切时间戳的字段date ,则只需运行

db.collection.remove({'date':{'$lt': new Date()} })

http://docs.mongodb.org/manual/reference/method/db.collection.remove/ http://docs.mongodb.org/manual/reference/method/db.collection.remove/

This will delete all the documents where the date is less than the current date. 这将删除日期小于当前日期的所有文档。 If you want, you can add the station_id field also. 如果需要,还可以添加station_id字段。 But for weather, I don't think that is necessary. 但是对于天气,我认为没有必要。

I would recommend to do this delete in a separate thread/cron job, instead of doing it when you are pushing the data. 我建议在单独的线程/ cron作业中执行此删除操作,而不是在推送数据时执行此删除操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM