我正在使用mongoose
使用此代码从mongoDb
数据库获取数据:
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var MG_forecast = mongoose.model('MG_forecast');
var MG_solarInfo = mongoose.model('MG_solarInfo');
router.get('/full_forecast', function(req, res, next) {
console.log("=---= full_forecast");
var day = new Date().getDate();
var month = new Date().getMonth()+1;
var year = new Date().getFullYear();
//recherche du champ forecast dans la collection MG_forecast
MG_forecast.find({"consult_day" : day, "consult_month":month, "consult_year" : year},function(err, docs){
if(err){
console.log("=---= full_forecast page : erreur GET , error : "+ err);
return next(err);
}
console.log("=---= full_forecast page : success GET ");
if (docs.length == 0){
console.log("=---= docs is empty !");
}
else {
console.log("=---= docs = " + docs);
}
res.json(docs);
});
});
module.exports = router;
这是模型
var mongoose = require("mongoose");
var MG_forecastSchema = new mongoose.Schema({
consult_date_ms : Number,
consult_day : Number,
consult_month : Number,
consult_year : Number,
forecast: [{
date: String, // looking like yyyy-mm-ddThh:mm:ss+UTC
temperature: Number
}]
});
mongoose.model('MG_forecast', MG_forecastSchema);
这是我想要检索的文件(它更长)
{
"_id":ObjectId("5630c6a2a31b5cd02987c4a4"),
"consult_date_ms":1446037154407,
"consult_day":28,
"consult_month":10,
"consult_year":2015,
"forecast":[
{
"date":"2015-10-28T15:00:00+01",
"temperature":13
},
{
"date":"2015-10-28T16:00:00+01",
"temperature":13
},
{
"date":"2015-10-28T17:00:00+01",
"temperature":12
},
{
"date":"2015-10-28T18:00:00+01",
"temperature":12
},
{
"date":"2015-10-28T19:00:00+01",
"temperature":12
},
{
"date":"2015-10-28T20:00:00+01",
"temperature":12
},
{
"date":"2015-10-28T21:00:00+01",
"temperature":13
},
{
"date":"2015-10-28T22:00:00+01",
"temperature":13
},
{
"date":"2015-10-28T23:00:00+01",
"temperature":13
},
{
"date":"2015-10-29T00:00:00+01",
"temperature":14
},
{
"date":"2015-10-29T01:00:00+01",
"temperature":14
},
{
"date":"2015-10-29T02:00:00+01",
"temperature":14
},
{
"date":"2015-10-29T03:00:00+01",
"temperature":14
},
{
"date":"2015-10-29T04:00:00+01",
"temperature":15
},
{
"date":"2015-10-29T05:00:00+01",
"temperature":15
},
{
"date":"2015-10-29T06:00:00+01",
"temperature":15
},
{
"date":"2015-10-29T07:00:00+01",
"temperature":15
},
{
"date":"2015-10-29T08:00:00+01",
"temperature":14
},
{
"date":"2015-10-29T09:00:00+01",
"temperature":14
},
{
"date":"2015-10-29T10:00:00+01",
"temperature":14
},
{
"date":"2015-10-29T11:00:00+01",
"temperature":15
},
{
"date":"2015-10-29T12:00:00+01",
"temperature":16
},
{
"date":"2015-10-29T13:00:00+01",
"temperature":17
},
{
"date":"2015-10-29T14:00:00+01",
"temperature":18
},
{
"date":"2015-10-29T15:00:00+01",
"temperature":18
},
{
"date":"2015-10-29T16:00:00+01",
"temperature":18
},
{
"date":"2015-10-29T17:00:00+01",
"temperature":17
},
{
"date":"2015-10-29T18:00:00+01",
"temperature":16
},
{
"date":"2015-10-29T19:00:00+01",
"temperature":16
},
{
"date":"2015-10-29T20:00:00+01",
"temperature":16
},
{
"date":"2015-10-29T21:00:00+01",
"temperature":16
},
{
"date":"2015-10-29T22:00:00+01",
"temperature":15
},
{
"date":"2015-10-29T23:00:00+01",
"temperature":15
},
{
"date":"2015-10-30T00:00:00+01",
"temperature":15
},
{
"date":"2015-10-30T01:00:00+01",
"temperature":15
},
{
"date":"2015-10-30T02:00:00+01",
"temperature":15
},
{
"date":"2015-10-30T03:00:00+01",
"temperature":14
},
{
"date":"2015-10-30T04:00:00+01",
"temperature":14
},
{
"date":"2015-10-30T05:00:00+01",
"temperature":14
},
{
"date":"2015-10-30T06:00:00+01",
"temperature":14
},
{
"date":"2015-10-30T07:00:00+01",
"temperature":14
},
{
"date":"2015-10-30T08:00:00+01",
"temperature":14
},
{
"date":"2015-10-30T09:00:00+01",
"temperature":14
},
{
"date":"2015-10-30T10:00:00+01",
"temperature":14
},
{
"date":"2015-10-30T11:00:00+01",
"temperature":15
},
{
"date":"2015-10-30T12:00:00+01",
"temperature":15
},
{
"date":"2015-10-30T13:00:00+01",
"temperature":17
},
{
"date":"2015-10-30T14:00:00+01",
"temperature":18
},
{
"date":"2015-10-30T15:00:00+01",
"temperature":18
},
{
"date":"2015-10-30T16:00:00+01",
"temperature":18
},
{
"date":"2015-10-30T17:00:00+01",
"temperature":17
},
{
"date":"2015-10-30T18:00:00+01",
"temperature":16
},
{
"date":"2015-10-30T19:00:00+01",
"temperature":15
},
{
"date":"2015-10-30T20:00:00+01",
"temperature":14
},
{
"date":"2015-10-30T21:00:00+01",
"temperature":13
},
{
"date":"2015-10-30T22:00:00+01",
"temperature":13
},
{
"date":"2015-10-30T23:00:00+01",
"temperature":12
},
{
"date":"2015-10-31T00:00:00+01",
"temperature":12
},
{
"date":"2015-10-31T01:00:00+01",
"temperature":12
},
{
"date":"2015-10-31T02:00:00+01",
"temperature":12
},
{
"date":"2015-10-31T03:00:00+01",
"temperature":11
},
{
"date":"2015-10-31T04:00:00+01",
"temperature":11
},
{
"date":"2015-10-31T05:00:00+01",
"temperature":10
},
{
"date":"2015-10-31T06:00:00+01",
"temperature":9
},
{
"date":"2015-10-31T07:00:00+01",
"temperature":9
},
{
"date":"2015-10-31T08:00:00+01",
"temperature":10
},
{
"date":"2015-10-31T09:00:00+01",
"temperature":10
},
{
"date":"2015-10-31T10:00:00+01",
"temperature":11
},
{
"date":"2015-10-31T11:00:00+01",
"temperature":13
},
{
"date":"2015-10-31T12:00:00+01",
"temperature":14
},
{
"date":"2015-10-31T13:00:00+01",
"temperature":15
},
{
"date":"2015-10-31T14:00:00+01",
"temperature":16
},
{
"date":"2015-10-31T15:00:00+01",
"temperature":17
},
{
"date":"2015-10-31T16:00:00+01",
"temperature":17
},
{
"date":"2015-10-31T17:00:00+01",
"temperature":16
},
{
"date":"2015-10-31T18:00:00+01",
"temperature":15
},
{
"date":"2015-10-31T19:00:00+01",
"temperature":14
},
{
"date":"2015-10-31T20:00:00+01",
"temperature":14
},
{
"date":"2015-10-31T21:00:00+01",
"temperature":14
},
{
"date":"2015-10-31T22:00:00+01",
"temperature":13
},
{
"date":"2015-10-31T23:00:00+01",
"temperature":13
},
{
"date":"2015-11-01T00:00:00+01",
"temperature":12
},
{
"date":"2015-11-01T01:00:00+01",
"temperature":12
}
]
}
我不知道为什么,但我得到的只是一个空数组,同时我的收藏中有一些东西(我用mongoDB
shell检查过),语法似乎正确。