简体   繁体   English

在Meteor应用中安排Mongo集合清理

[英]Schedule Mongo collection cleanup in Meteor app

So I've created an app that basically allows my teammates to fill out and submit forms. 因此,我创建了一个应用程序,基本上可以让我的队友填写并提交表格。 In original testing, Meteor would randomly refresh the page (even though no dependent data sources had been updated), and they would lose the info already entered in the form. 在原始测试中,Meteor将随机刷新页面(即使没有更新任何依赖的数据源),它们也会丢失已经在表格中输入的信息。 So I hacked together a two-way data binding solution by making the app generate a temporary document, in a separate Mongo collection, that would store the form data until the user completes and submits the form -- at which point, the app removes the temporary document. 因此,我通过使应用程序在一个单独的Mongo集合中生成一个临时文档,从而破解了双向数据绑定解决方案,该文档将存储表单数据,直到用户完成并提交表单为止-此时,该应用程序将删除临时文件。 I also used iron router to dynamically generate a unique page using the new document's id. 我还使用Iron路由器使用新文档的ID动态生成唯一页面。

My problem is that, if the user doesn't submit or cancel the form, the document doesn't get removed, and I'm left with a bunch of unnecessary data. 我的问题是,如果用户不提交或取消表单,那么该文档也不会被删除,而我剩下的是一堆不必要的数据。 I want to schedule a weekly clean up of that Meteor collection, but I have no idea how to do this. 我想每周清理一次Meteor集合,但是我不知道该如何做。 I'm not sure what code is relevant here, but I've included the event that creates and removes the temporary document: 我不确定这里是否有相关的代码,但是我包含了创建和删除临时文档的事件:

Screens = new Meteor.Collection('screens') // where forms will ultimately be stored
Forms = new Meteor.Collection('forms') // form templates
NewScreen = new Meteor.Collection('newscreen') //used for temporary data storage

//event that generates temporary object when users start new form
'click [name=new-screen]': function(e, tmpl) {
      NewScreen.insert({
        first: '',
        role: this.position_name,
        answers: [],
        form_id: this._id,
        position_name: this.position_name,
        form_bundle: this.form_bundle,
        created_at: new Date
      });


//event that stores form data in the final collection, and removes the temporary storage document

'click [name=submit]': function(e, tmpl) {
      e.preventDefault();

      var newObj = {};
      var q_elements = $('.question-form li');
      var a_elements = $('textarea');
      var ca_elements = $('.correct-answer');
      var qa_bundle = []

      for(i=0; i<q_elements.length; i++){
        myObj={}
        myObj['question'] = q_elements[i].innerHTML;
        myObj['answer'] = a_elements[i+1].value;
        myObj['correct_answer'] = ca_elements[i].innerHTML.split('</b> ')[1];
        qa_bundle.push(myObj);
      }

      newObj['name'] = $('input')[0].value;
      newObj['prescreen_notes'] = $('#prescreen-notes')[0].value;
      newObj['role'] = this.position_name;
      newObj['qa_bundle'] = qa_bundle;
      newObj['created_at'] = new Date;


      Screens.insert(newObj);

      for(i=0;i<$('input').length; i++){
        $('input')[i].value = '';
      }

      for(i=0;i<$('textarea').length; i++){
        $('textarea')[i].value = '';
      }

      $('#new-id')[0].innerHTML = 'Link to candidate prescreen: <a href="/screens/' + Screens.find().fetch()[Screens.find().fetch().length-1]._id +'">' + Screens.find().fetch()[Screens.find().fetch().length-1]._id + '</a>';

      NewScreen.remove({_id: window.location.pathname.split('/')[window.location.pathname.split('/').length-1]})  
    },

If it helps, I've committed the version of the app that the "meteor build" command spits out to make it a node.js app, to a github repo here: https://github.com/gharezlak/prescreens 如果有帮助,我将“流星构建”命令吐出的应用程序版本提交到github仓库中,以使其成为一个node.js应用程序: https : //github.com/gharezlak/prescreens

I think you can set a timeout event when user ask for the form. 我认为您可以在用户要求表单时设置超时事件。 Event time can be hours or days based on you application logic. 根据您的应用程序逻辑,事件时间可以是数小时或数天。

When even is raised, you will check if form was submitted, and clean single document if needed. 当提高偶数时,您将检查是否提交了表单,并根据需要清理单个文档。

Use the synced-cron package 使用sync-cron程序包

SyncedCron.add({
  name: 'Cleanup NewScreen Colleciton',
  schedule: function(parser) {
    return parser.text('every weekend');
  },
  job: function() {
    var date = new Date();
    date.setDate(date.getDate() - 1);  // 1 day ago
    NewScreen.remove({createdAt: { $lt: date }})
  }
});

However, you may be better off storing these temporary values clientside in localstorage. 但是,最好在客户端将这些临时值存储在本地存储中。

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

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