node.js - Node MongoDB Native - filter by date/time -


in native mongodb, following limits full text search articles published in 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 not work on node.js node-mongodb-native - whole $where object ignored, doesn't throw error. there other way can limit query results based on time?

try creating datetime object prior using in query constructing new date value of current timestamp minus 6 hours 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 use momentjs library pretty handy when dealing dates, in particular subtract() method

var moment = require('moment'),     sixhoursago = moment().subtract(6, 'hours'); 

Comments

Popular posts from this blog

php - Wordpress: Link a post to posts of another category manually -

javascript - Complete OpenIDConnect auth when requesting via Ajax -

apache - PHP Soap issue while content length is larger -