node.js Mongoose 按日期查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40332455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:48:15 来源:igfitidea点击:
Mongoose query by date
提问by mdakovac
I want to query mongoDB with document structure like this:
我想用这样的文档结构查询 mongoDB:
var ExampleSchema = mongoose.Schema({
createdAt: { type: Date, default: Date.now },
validUntil: Date,
name: String
});
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:
并且需要它只返回有效文档,即 validUntil 大于当前时间的地方。这不起作用,猫鼬返回所有文件:
var d = new Date();
var n = d.toISOString();
Example.find({ '$where': 'validUntil'>n })
回答by Bertrand Martel
Use $gtelike this :
$gte像这样使用:
Example.find({
validUntil: {
$gte: new Date(2016,09,30)
}
})
回答by Tính Ng? Quang
回答by Abderrahim Soubai Elidrissi
get all created account today:
立即获取所有创建的帐户:
let start = new Date(now.getFullYear(),now.getMonth(),now.getDate(),1,0,0);
let end = new Date(now.getFullYear(),now.getMonth(),now.getDate()+1,0,59,59);
let query = {createdAt: {$gte: start, $lt: end} };
Account
.find(query)
.exec((err, accounts) => console.log(account) )

