MongoDB Mongoose select documents between a date range

做~自己de王妃 提交于 2021-02-05 09:32:29

问题


I've a collection called events. Sample document is like this.

I want to select documents with in date range of '2019-01-11' to '2019-11-27'. What I've done is this. But this seems not working. How do I achieve this using MongoDB Mongoose?

$match: {
                    createdAt: {
                        $gte: {
                            $dayOfYear: '2019-01-11'
                        },
                        $lte: {
                            $dayOfYear: '2019-11-27'

                        }
                    }
                }

回答1:


Use the Date() object instead. I am not sure if this affects the timezone as well.


  db.collection.find({
                        "createdAt": { 
                                       "$gte": new Date('2019, 1, 11'), 
                                       "$lt" : new Date(2019, 11, 27)
                                      }
                     })



来源:https://stackoverflow.com/questions/59066544/mongodb-mongoose-select-documents-between-a-date-range

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!