Reputation: 4285
In native MongoDB, the following limits my full text search to articles published in the past 6 hours:
db.getCollection('articles').find({
$and: [{
$text: {
$search: 'stackoverflow'
}
}, {
$where: function() {
return Date.now() - this.published.getTime() < (6 * 60 * 60 * 1000)
}
}]
})...
The above does not work on Node.js with node-mongodb-native - the whole $where object is ignored, but doesn't throw an error. Is there any other way I can limit my query results based on time?
Upvotes: 0
Views: 1563
Reputation: 103375
Try creating the datetime object prior to using it in your query by constructing a new Date with the value of the current timestamp minus 6 hours as follows:
var sixHours = 6 * 60 * 60 * 1000; /* ms */
sixHoursAgo = new Date(new Date().getTime() - sixHours);
db.getCollection('articles').find({
$text: { $search: 'stackoverflow'},
published: { $gte: sixHoursAgo }
})
Or you could use the momentjs library which is pretty handy when dealing with dates, in particular the subtract()
method
var moment = require('moment'),
sixHoursAgo = moment().subtract(6, 'hours');
Upvotes: 2