javascript - Mongoose update by date without time - Stack Overflow

I'm using mongoose 4.7.x and mongodb 3.2. I want to search all date without time. E.g update all o

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 badges
Add a ment  | 

1 Answer 1

Reset to default 5

You 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

相关推荐

  • javascript - Mongoose update by date without time - Stack Overflow

    I'm using mongoose 4.7.x and mongodb 3.2. I want to search all date without time. E.g update all o

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信