按日期进行的Mongoose查询

时间:2016-10-30 18:54:15

标签: node.js mongodb mongodb-query

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

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

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

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

3 个答案:

答案 0 :(得分:13)

像这样使用$gte

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

答案 1 :(得分:2)

如果今天使用momentjs

// start today
var start = moment().startOf('day');
// end today
var end = moment(today).endOf('day');

Example.find({ validUntil: { '$gte': start, '$lte': end })

答案 2 :(得分:0)

立即获取所有创建的帐户:

delete from table
 ( select row_number() over (partition by region) as PAR 
   from sales
 )b  
where PAR >= 
 ( select S*0.1 as ninety
   from 
    ( select region, count(*) as S 
      from sales 
      group by region
    )a
and b.region = a.region