我正在使用nodejs和moongose来处理mongodb数据库。我需要基本上读取信息,字段_id来获取时间戳和字段temp,记录的一个例子:
Mongodb记录示例
{ _id: 57fe867e473a802218a891e0,
_etag: 57fe867e225e107c6f47dc55,
temp: 27875,
coord: [ [Object] ] }
我正在使用以下代码执行读取:
阅读代码
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
ObjectID = require('mongodb').ObjectID;
var configDB = require('./config/database.js');
mongoose.connect(configDB.url);
mongoose.model('Temperatura',
new Schema({ time: Number, temperature: Number}),
'col');
var dados = mongoose.model('Temperatura');
dados.find({}, function(err, data)
{
if(err){
console.log(err, data, data.length);
}else{
console.log(data);
}
var Time = [];
var Temperature = [];
var Grafico = [];
for ( index in data){
var doc = data[index];
var objectId = new ObjectID(doc['_id'])
var time = objectId.getTimestamp();
console.log(doc['temp']);
var temperature = doc['temp'];
Temperature.push({"value" : temperature});
Time.push({"value" : time});
}
}).sort('-date').limit(100);
基本问题是只有Time
数组有值,Temperature
数组显示未定义的值,代码错误是什么?
答案 0 :(得分:1)
你的猫鼬模型需要匹配mongodb记录中的实际数据。尝试在模型中添加临时字段:
mongoose.model('Temperatura',
new Schema({ time: Number, temp: Number}),
'col');