简体   繁体   English

如何只保留 Firebase 中的几个元素?

[英]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.

相关问题 如何获取 firebase 集合中的元素个数? - How to get the number of elements in a firebase collection? Map 一些带有 NextJs 的文档和 Firebase - Map few docs with NextJs and Firebase Firebase 和检索元素 - Firebase & Retrieving Elements 我如何只知道 Button OnClickListener 中的用户 ID 就可以向特定用户发送推送通知? Firebase - How can i send push notification to specific users just knowing the userID inside Button OnClickListener? Firebase 如何从 firebase 示例中删除一项,我只想从子项中删除“childid1” - how to remove one item from firebase example, I want to remove just "childid1" from children Firebase 如果设备闲置几分钟且未充电,则推送通知未发送 state - Firebase push notification not delivered if device is idle for few minutes and no charging state 为什么 Firebase Deploy 刚刚找到 1-2 文件? 只是 Firebase 托管的默认索引 - Why Firebase Deploy just found 1-2 file? Just default index of Firebase hosting ListTile 显示所有元素,而不仅仅是需要的元素 - ListTile showing all elements rather than just the elements that are needed 如何获取我刚刚在 Node.js 上使用 firebase 身份验证创建的用户的令牌 ID? - How do I get the token ID of a user that I just created using firebase authentication on Node js? 反应本机 firebase setBackgroundMessageHandler 刚刚在前台调用 - react native firebase setBackgroundMessageHandler invoked just in forground
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM