I want to query mongoDB with document structure like this:
var ExampleSchema = mongoose.Schema({
createdAt: { type: Date, default: Date.now },
validUntil: Date,
name: String
});
and need it to return only valid documents, i.e. where validUntil is greater than current time. This doesn't work, mongoose returns all documents:
var d = new Date();
var n = d.toISOString();
Example.find({ '$where': 'validUntil'>n })
Use $gte
like this :
Example.find({
validUntil: {
$gte: new Date(2016,09,30)
}
})
If find in today, using momentjs
// start today
var start = moment().startOf('day');
// end today
var end = moment(today).endOf('day');
Example.find({ validUntil: { '$gte': start, '$lte': end })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With