I'm using mongoose 4.7.x and mongodb 3.2. I want to search all date without time. E.g update all obj with this date 2016-12-14
.
In my database I have multiple datetime like this :
2016-12-14 00:00:00.000Z
2016-12-14 00:00:00.000Z
2016-12-14 01:00:00.000Z
2016-12-14 01:00:00.000Z
2016-12-14 02:00:00.000Z
2016-12-14 02:00:00.000Z
I tried this :
var query = {date: new Date('2016-12-14')};
var doc = {name: 'TEST'};
var options = {multi: true};
Model.update(query, doc, options, function(err, result){
console.log('All data updated')
});
But this will just update all fields with time 00:00:00 because new Date()
will returns the datetime.
How can I do to search all fields with a certain date without time ? I can change the model if it's necessary.
I'm using mongoose 4.7.x and mongodb 3.2. I want to search all date without time. E.g update all obj with this date 2016-12-14
.
In my database I have multiple datetime like this :
2016-12-14 00:00:00.000Z
2016-12-14 00:00:00.000Z
2016-12-14 01:00:00.000Z
2016-12-14 01:00:00.000Z
2016-12-14 02:00:00.000Z
2016-12-14 02:00:00.000Z
I tried this :
var query = {date: new Date('2016-12-14')};
var doc = {name: 'TEST'};
var options = {multi: true};
Model.update(query, doc, options, function(err, result){
console.log('All data updated')
});
But this will just update all fields with time 00:00:00 because new Date()
will returns the datetime.
How can I do to search all fields with a certain date without time ? I can change the model if it's necessary.
Share Improve this question edited Dec 14, 2016 at 14:36 John asked Dec 14, 2016 at 14:26 JohnJohn 5,00110 gold badges53 silver badges107 bronze badges1 Answer
Reset to default 5You can do it in between a range of dates. Use $gte
for the lower range, and $lt
for the higher one (which is the next day) to keep it from selecting the next day at 00:00:00 hours.
var query = {
date: {
$gte: new Date('2016-12-14'),
$lt: new Date('2016-12-15')
}
};
var doc = {name: 'TEST'};
var options = {multi: true};
Model.update(query, doc, options, function(err, result){
console.log('All data updated')
});
All documents between 2016-12-14 00:00:00.000Z and 2016-12-14 23:59:59.999Z will be updated.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744736282a4590766.html
评论列表(0条)