MongoDB Mongoose 在日期范围内选择文档 [英] MongoDB Mongoose select documents between a date range

查看:53
本文介绍了MongoDB Mongoose 在日期范围内选择文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 events 的集合.示例文档是这样的.

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

我想选择日期范围为'2019-01-11''2019-11-27'的文档.我所做的就是这个.但这似乎不起作用.我如何使用 MongoDB Mongoose 实现这一点?

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'

                        }
                    }
                }

推荐答案

改用 Date() 对象.我不确定这是否也会影响时区.

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)
                                      }
                     })

这篇关于MongoDB Mongoose 在日期范围内选择文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆