[英]How to mantain just a few elements in Firebase?
I'm doing an app with a Firebase Realtime Database connection, the issue is that I'll use Arduino to send measured information to firebase and visualize this in the app and I don't want to maintain all the information in the DB, just for example the last 10 sensed values.我正在使用 Firebase 实时数据库连接做一个应用程序,问题是我将使用 Arduino 将测量信息发送到 firebase 并在应用程序中将这些信息可视化,我不想在 DB 中维护所有信息例如最后 10 个感测值。 The DB is structured this way,
数据库的结构是这样的,
Root "Humidity" Datetime1 (Hour/minute/second Day-Month-year): Sensed Value 1 Datetime2 (Hour/minute/second Day-Month-year): Sensed Value 2... Etc.根“湿度” Datetime1(时/分/秒日-月-年):感测值 1 Datetime2(时/分/秒日-月-年):感测值 2... 等等。
How can achive that?怎么能做到呢? i dont want to do it in the app, i would like to do it in firebase itself (cause the users of the app are not going to be all the time in the app, and arduino is gonna still sending info)
我不想在应用程序中进行,我想在 firebase 本身中进行(因为应用程序的用户不会一直在应用程序中,arduino 仍然会发送信息)
I hope that you can help me, thanks in advance!我希望你能帮助我,在此先感谢!
Instead of creating your schema like而不是像创建您的架构
+ Humedad
+ time_stamp_1: value
+ time_stamp_2: value
Do it something like,做类似的事情,
+ Humedad
+ static_id_1
+ timestamp: value
+ reading: value
+ static_id_2
+ timestamp: value
+ reading: value
While updating / inserting these readings you loop for ten times on to rewrite,在更新/插入这些读数时,您循环十次以重写,
["static_id_1", "static_id_2", "static_id_3", ..."static_id_10"]
these values and reset the loop in your arduino again to rewrite on same keys.这些值并再次重置 arduino 中的循环以重写相同的键。
Please do mention which library you are using to communicate with firebase for detailed explanation.请务必提及您使用哪个库与 firebase 进行通信以获取详细说明。
I don't want to do it in the app.
我不想在应用程序中这样做。
If you always need to have only 10 children available under a certain node, there is no need to do that in your application code.如果您总是需要在某个节点下只有 10 个子节点可用,则无需在应用程序代码中执行此操作。 The simplest solution would be to write a function in Cloud Functions for Firebase , and each time the 11th element is written under that node, delete the oldest one.
最简单的解决方案是在Cloud Functions 中为 Firebase 编写 function ,并且每次在该节点下写入第 11 个元素时,删除最旧的元素。 In this way, you'll only have 10 children.
这样,您将只有 10 个孩子。
If you intend in the future to create some statistics, then you should consider leaving the data as it is, and only retrieve the latest 10. Here's the official documentation regarding filtering data .如果您将来打算创建一些统计信息,那么您应该考虑保持数据不变,只检索最新的 10 个。这里是关于过滤数据的官方文档。 So Query#limitToLast(int limit) will do the trick.
所以Query#limitToLast(int limit)可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.