Javascript 无法使用mongoose按日期获取集合
我无法按日期查询集合筛选 下面的代码是我到目前为止的代码Javascript 无法使用mongoose按日期获取集合,javascript,html,node.js,mongoose,Javascript,Html,Node.js,Mongoose,我无法按日期查询集合筛选 下面的代码是我到目前为止的代码 //The following code shows how my schema is: date: {type: Date, required: true} //This is a date from a collection in MongoDB: date: 2019-09-06T16:48:14.000+00:00 //This is how I saved the date in the MongoDB: "2019/09
//The following code shows how my schema is:
date: {type: Date, required: true}
//This is a date from a collection in MongoDB:
date: 2019-09-06T16:48:14.000+00:00
//This is how I saved the date in the MongoDB:
"2019/09/09 08:55:15"
//And this is how I perform the query in NodeJS:
let year = req.query.year;
let month = req.query.month;
let day = req.query.day;
let startDate = new Date(year, month, day);
let endDate = new Date(year, month, day);
// find documents in MongoDB
let query = SALES.find({ date: { $gte: startDate, $lte: endDate }});
// execute the query at a later time
query.exec(function (err, item) { // item is a dictionary
if (err) return handleError(err); // throws an error if any
if (item === null || item.length === 0) {
res.json({
status: 'empty',
});
}
else {
res.json({
salesRecord: item
});
}
});
我读到的东西很容易得到,但我做不到。欢迎任何帮助简短回答 您保存的日期(“2019/09/09 08:55:15”)将被视为字符串 试试这个:
db.mycollection.find({
"date" : {"$gte": new Date()}
})
或
说明
Mongodb shell提供了各种方法来返回日期。它可以是字符串,也可以是日期对象:
简短回答 您保存的日期(“2019/09/09 08:55:15”)将被视为字符串 试试这个:
db.mycollection.find({
"date" : {"$gte": new Date()}
})
或
说明
Mongodb shell提供了各种方法来返回日期。它可以是字符串,也可以是日期对象:
您可以显示您的
req.query
吗?req.query有一些值,例如:year=2019,month=08,day=09,等等。您的startDate
和endDate
具有相同的值,因此数据库中的记录需要具有与新日期(年、月、日)完全相同的日期。
是的,它们有,并且查询不起作用;(您能显示您的req.query
吗?req.query有一些值,例如:year=2019,month=08,day=09,等等。您的startDate
和endDate
具有相同的值,因此数据库中的记录需要与新日期(年、月、日)完全相同。
是的,它们有,查询不起作用(