Angular Firestore观察和接收子文档而不是整个文档?(时间序列数据)
我有一个Firebase Firestore数据库,其中包含表示时间段的文档。每小时都会创建一个新文档来存储我的物联网传感器读数 每个文档存储:Angular Firestore观察和接收子文档而不是整个文档?(时间序列数据),angular,firebase,google-cloud-firestore,observable,Angular,Firebase,Google Cloud Firestore,Observable,我有一个Firebase Firestore数据库,其中包含表示时间段的文档。每小时都会创建一个新文档来存储我的物联网传感器读数 每个文档存储: 最近湿度 最近温度 一个小时内所有湿度和温度读数的数组 我不想多次收到一系列历史测量结果。是否可以观察文档中某个元素的更改并仅接收这些子部分的更新?还是有更好的方法来存储我的时间序列数据?谢谢大家! 供参考: 在ngOnInit中,我有一个简单的文档get(): 我的插入/更新代码: insertData() { let docName =
- 最近湿度
- 最近温度
- 一个小时内所有湿度和温度读数的数组
insertData() {
let docName = this.buildFormattedDateTime(); // example: '2021_05_12_16h'
this.firestore.collection('officePlants').doc(docName).set({
'humidity': this.humiditySensorReading,
'temperature': this.temperatureSensorReading,
'historicalMeasurements': firebase.firestore.FieldValue.arrayUnion({
'humidity': this.humiditySensorReading,
'temperature': this.temperatureSensorReading,
'timestamp': firebase.firestore.Timestamp.now()
})
},
{merge: true}); // 'merge: true' provides an update and creates the doc if it doesn't exist
}
示例文件:
/officePlants/2021_05_12_16h/
我愿意接受所有的选择
是否可以观察文档中某个元素的更改并仅接收这些子部分的更新
Firestore侦听器在文档级别进行操作。因此,如果文档中发生了更改,您将收到整个文档
如果您想接收较少的数据,请考虑在每个文档中创建一个子集合,其中将数据扩展到多个文档。
或者,您可以让主文档只包含最新的读数,并为该小时的所有历史记录创建一个历史记录
文档
2021\u 05\u 11\u 22h\u最新版本
2021\u 05\u 11\u 22h\u满
insertData() {
let docName = this.buildFormattedDateTime(); // example: '2021_05_12_16h'
this.firestore.collection('officePlants').doc(docName).set({
'humidity': this.humiditySensorReading,
'temperature': this.temperatureSensorReading,
'historicalMeasurements': firebase.firestore.FieldValue.arrayUnion({
'humidity': this.humiditySensorReading,
'temperature': this.temperatureSensorReading,
'timestamp': firebase.firestore.Timestamp.now()
})
},
{merge: true}); // 'merge: true' provides an update and creates the doc if it doesn't exist
}
{
humidity: 70,
temperature: 40,
historicalMeasurements: [
{
humidity: 71,
temperature: 41,
timestamp: 2021-05-12 11:00:00
},
{
humidity: 72,
temperature: 40,
timestamp: 2021-05-12 11:01:00
},
...
]
}