简体   繁体   中英

Meteor update when time meets collection value

In my mongoDB I got a collection called Questions. Each question has got a date, time and a display true or false. Now, I want to update the question when current date is met, to Display: true.

I dont know how I can update a collection without having to fire an event, so I assume it has to work on the server side.

Right now this is on my server side to check if it works with seconds (instead of full date), but it has only updated one question, and I know this is not the right way.

Meteor.startup(function () {
    return Question.update({
        time: 40
    },{
        $set: {display: true}
})

To make it some more clear:

If current time meets with time from question in collection, set display: true.

I hope someone can help me.

If your goal is only to display or not display a document based on time then you don't actually need to alter the document at a specified time. You can make time itself reactive and display based on that. Look at the remcoder:chronos package.

You could do this in your query condition for example:

Template.myTemplate.helpers({
  myDocuments: function(){
    var now = Chronos.currentTime(); // updates every second by default
    return MyCollection.find({ time: { $gt: now }});
  }
});

This is much cleaner than altering the documents themselves.

You want to use Meteor.setTimeout .

Your code should look something like this:

Meteor.setTimeout(function () {
  Meteor.update({ _id: someId }, { $set: { display: false } }
}, 1000); // will run after 1000ms

You can choose any selector you want to decide which document(s) to update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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