admin管理员组文章数量:1391955
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.
本文标签: javascriptMongoose update by date without timeStack Overflow
版权声明:本文标题:javascript - Mongoose update by date without time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744736282a2622348.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论