Make mongo queries with date condition reactive?

I can’t seem to make my mongo query with date condition reactive.

Is there a way to make something like this reactive?

‘date’: { $lt: new Date() }}

When I do this, the “condition” for “$lt: new Date()” was never reactive. Therefore the query result never updates itself reactively.

Is there a way to make this reactive somehow?

The cursor returned by Doc.find() is reactive, meaning that if you use it inside an autorun or a template helper, the original query (with original parameter values) will be run again whenever the query result changes.

So my guess is that your problem does not relate to the query being reactive or not, but rather to how to track a changing date in a reactive manner. Maybe you can tell us more about your need? What do you want to achieve?

Well, you explained it well.

Doc.find() is reactive. But I can’t think of a ideal solution for making “new Date()” reactive.

If Doc.find() is executed, the timestamp at the time of execution is fixed and won’t change until the next time Doc.find() is reactively re-executed. However as far as I can tell, there’s no way to trigger Meteor’s reactive nature unless I use a session or autorun – and using a timer to re-execute the query seems like an incredibly bad solution.

I guess one solution is to do a timer that triggers reactivity every 5-10 minutes. But is there a better way?

The javascript function new Date is not reactive. You need to store the date in a reactive variable, like a Session or ReactiveVar

Doc.find({date: {$lt: Session.get('myDate') }});

should do the trick, if you Session.set(‘myDate’, new Date()) before calling Doc.find for the first time.