我想将传感器数据存储在MongoDB中,比如频率为1hz。为了有效地执行此操作,我希望以this article recommends的方式执行此操作,即使用作为对象属性所经过的秒数在同一文档中存储多个数据点。
这就是我想要一个典型文档的样子:
const record = {
startingHour: 'Sun Sep 11 2016 03:00:00', // Date object
values: {
0: { // minute
0: { lat: 52.0001, lon: 13.0001 },
// ... all other seconds
59: { lat: 52.9999, lon: 13.9999 }
},
// ... all other minutes
59: {
0: { lat: 53.0001, lon: 14.0001 },
// ...
59: { lat: 53.9999, lon: 14.9999 }
}
}
}
我如何在Mongoose中指定我希望每个文档代表一个包含60个属性的小时,每个键是一个从1到59的数字 - 每分钟一个 - 并且每个属性依次具有60个属性,并且那些记录代表实际的位置值?
为了保存新记录,我会record.values.59.59 = { lat: 53.9999, lon: 14.9999 }
我怎么能在Mongoose中表示这种模式?
答案 0 :(得分:0)
我设法通过创建函数来生成我想要的对象。然后我将这些生成的对象用作模式,将一个嵌入到另一个中并将它们放在最终模式中:
/**
* Functions to generate objects for Minutes and Seconds
* Each object containing 60 keys, going from 0 to 59
*/
function second() {
let obj = {};
for (var i = 0; i < 60; i++) {
obj[i] = Location;
}
return obj;
}
function minute() {
let obj = {};
for (var i = 0; i < 60; i++) {
obj[i] = Second;
}
return obj;
}
const Location = new Schema({
latitude: { type: Number, required: true },
longitude: { type: Number, required: true }
});
// Instantiates Second and Minute objects as schemas
const Second = new Schema(second());
const Minute = new Schema(minute());
// Actual schema
const RecordSchema = new Schema({
startingHour: {
type: Date,
required: true
},
values: {
type: Minute
}
});
这允许我保存这样的对象:
{
startingHour: new Date(),
values: {
0: {
0: {
location: { latitude: 0.03, longitude: 51.7 }
},
1: {
location: { latitude: 0.03, longitude: 51.7 }
}
// ...
},
// ...
59: {
// ...
59: {
location: { latitude: 0.03, longitude: 51.7 }
}
}
}
}