Node.js 查找包含字段日期的MongoDB记录<;日期:现在()
所以我把机器人应用程序连接到了MongoDB。当我跑步时:Node.js 查找包含字段日期的MongoDB记录<;日期:现在(),node.js,mongodb,mongoose,mongoose-schema,Node.js,Mongodb,Mongoose,Mongoose Schema,所以我把机器人应用程序连接到了MongoDB。当我跑步时: db.getCollection('bookings').find({"date" : { $lte : new Date("<YYYY-mm-ddTHH:MM:ssZ>")}}) 这是我当前的代码: router.get('/past', function(req, res) { //retrieve all bookings from Monogo mongoose.model('Booking').find(
db.getCollection('bookings').find({"date" : { $lte : new Date("<YYYY-mm-ddTHH:MM:ssZ>")}})
这是我当前的代码:
router.get('/past', function(req, res) {
//retrieve all bookings from Monogo
mongoose.model('Booking').find({"date" : { $lte : new Date("<YYYY-mm-ddTHH:MM:ssZ>")}}, function (err, bookings) {
if (err) {
return console.error(err);
} else {
//respond to both HTML and JSON. JSON responses require 'Accept: application/json;' in the Request Header
res.format({
//HTML response will render the index.jade file in the views/bookings folder. We are also setting "bookings" to be an accessible variable in our jade view
html: function(){
res.render('bookings/index', {
title: 'All my Bookings',
"bookings" : bookings
});
},
//JSON response will show all bookings in JSON format
json: function(){
res.json(bookings);
}
});
}
});
});
router.get('/pass',函数(req,res){
//从Monogo检索所有预订
mongoose.model('Booking').find({“date”):{$lte:newdate(“”}},函数(err,bookings){
如果(错误){
返回控制台。错误(err);
}否则{
//响应HTML和JSON。JSON响应要求在请求头中输入“Accept:application/JSON;”
存储格式({
//HTML响应将在views/bookings文件夹中呈现index.jade文件。我们还将“bookings”设置为jade视图中的可访问变量
html:function(){
res.render(‘预订/索引’{
标题:“我所有的预订”,
“预订”:预订
});
},
//JSON响应将以JSON格式显示所有预订
json:function(){
res.json(预订);
}
});
}
});
});
第一次使用Mongoose,如果这很简单,请道歉。您是否尝试过
查找({“日期”:{$lte:new date()}},…
?提示:点击F12,进入开发者控制台并编写var test=new date(“”)
…然后编写test
,它将显示您的类似内容[date]无效日期[date]无效日期
router.get('/past', function(req, res) {
//retrieve all bookings from Monogo
mongoose.model('Booking').find({"date" : { $lte : new Date("<YYYY-mm-ddTHH:MM:ssZ>")}}, function (err, bookings) {
if (err) {
return console.error(err);
} else {
//respond to both HTML and JSON. JSON responses require 'Accept: application/json;' in the Request Header
res.format({
//HTML response will render the index.jade file in the views/bookings folder. We are also setting "bookings" to be an accessible variable in our jade view
html: function(){
res.render('bookings/index', {
title: 'All my Bookings',
"bookings" : bookings
});
},
//JSON response will show all bookings in JSON format
json: function(){
res.json(bookings);
}
});
}
});
});