Mongoose 按日期查询 [英] Mongoose query by date

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

问题描述

我想用这样的文档结构查询 mongoDB:

I want to query mongoDB with document structure like this:

var ExampleSchema = mongoose.Schema({
    createdAt: { type: Date, default: Date.now },
    validUntil: Date,
    name: String
});

并且需要它只返回有效文档,即 validUntil 大于当前时间.这不起作用,猫鼬返回所有文档:

and need it to return only valid documents, i.e. where validUntil is greater than current time. This doesn't work, mongoose returns all documents:

var d = new Date();
var n = d.toISOString();
Example.find({ '$where': 'validUntil'>n })

推荐答案

像这样使用 $gte :

Example.find({
    validUntil: {
        $gte: new Date(2016,09,30)
    }
})

这篇关于Mongoose 按日期查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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