无法使用日期(nodejs)使用猫鼬查询数据
Unable to query data with mongoose using dates (nodejs)
我正在尝试查询在开始日期和结束日期之间具有“inputDate”的所有协助文档。我已经在互联网上寻找了很长时间,但我无法让它发挥作用。它应该检索现在在我的数据库中的唯一文档。我正在使用 NodeJS+Mongoose。
控制器中的代码如下:
exports.getOccupation = (req, res, next) => {
var inputDate = new Date("1999","10","13","16","0","0");
Assistance.find({
dateInterval:{
startDate: {$lte:inputDate},
endDate: {$gte:inputDate}
}
})
.then(assistances => {
console.log(assistances.length);
})
}
型号代码:
const assistanceSchema = new Schema(
{
user_id:{
type: Schema.ObjectId
},
place:{
longitude:{
type:String
},
latitude:{
type:String
}
},
dateInterval:{
startDate: Date,
endDate: Date
}
},
{timestamps:true}
);
我不确定,但我相信 find() 方法接受 dateInterval.startDate 而不是 dateInterval:{startDate: ... }.
试试这样改:
Assistance.find({
$and: [
{ "dateInterval.startDate": { $lte: inputDate } },
{ "dateInterval.endDate": { $lte: inputDate } },
],
}).then((assistances) => {
console.log(assistances.length);
});
我正在尝试查询在开始日期和结束日期之间具有“inputDate”的所有协助文档。我已经在互联网上寻找了很长时间,但我无法让它发挥作用。它应该检索现在在我的数据库中的唯一文档。我正在使用 NodeJS+Mongoose。
控制器中的代码如下:
exports.getOccupation = (req, res, next) => {
var inputDate = new Date("1999","10","13","16","0","0");
Assistance.find({
dateInterval:{
startDate: {$lte:inputDate},
endDate: {$gte:inputDate}
}
})
.then(assistances => {
console.log(assistances.length);
})
}
型号代码:
const assistanceSchema = new Schema(
{
user_id:{
type: Schema.ObjectId
},
place:{
longitude:{
type:String
},
latitude:{
type:String
}
},
dateInterval:{
startDate: Date,
endDate: Date
}
},
{timestamps:true}
);
我不确定,但我相信 find() 方法接受 dateInterval.startDate 而不是 dateInterval:{startDate: ... }.
试试这样改:
Assistance.find({
$and: [
{ "dateInterval.startDate": { $lte: inputDate } },
{ "dateInterval.endDate": { $lte: inputDate } },
],
}).then((assistances) => {
console.log(assistances.length);
});