【问题标题】:Correct way to structure firebase database by dates按日期构建firebase数据库的正确方法
【发布时间】:2020-05-09 00:37:39
【问题描述】:

所以我在我的服务器上监控了几个对象,并且每个小时我都想将这些对象的读数发布到我的数据库中。我需要一种简单的方法来获取这些数据,以便我可以按数组中的日期对它们进行排序,轻松获取特定日期的值,并清理特定日期不再需要的旧数据。

我目前对如何构建数据有两个想法:

1) 按日期分块

objects
    - objectA
        - 2020
            - 01
                - 22
                    - 01
                        - property1 : "Value at 1 o'clock on the 22nd of Jan, 2020"
                        - property2 : "Value at 1 o'clock on the 22nd of Jan, 2020"
                    - 02
                        - property1 : "Value at 2 o'clock on the 22nd of Jan, 2020"
                        - property2 : "Value at 2 o'clock on the 22nd of Jan, 2020"
                    ...
    - objectB
        ...

这种方法让我可以轻松查询特定日期或月份并从中获取值。它还可以让我轻松清除特定日期或月份的旧数据。

但缺点是我想不出一种简单的方法来查询它以将我的应用程序中的数据作为按时间排序的平面列表(在大多数情况下,这就是我需要数据的方式)

2) 将数据存储为属性的平面日期列表

objects
    - objectA
        - 2020:01:22:01
            - property1 : "Value at 1 o'clock on the 22nd of Jan, 2020"
            - property2 : "Value at 1 o'clock on the 22nd of Jan, 2020"
        - 2020:01:22:02
            - property1 : "Value at 2 o'clock on the 22nd of Jan, 2020"
            - property2 : "Value at 2 o'clock on the 22nd of Jan, 2020"
                    ...
    - objectB
        ...

此模型可以更轻松地以我需要它们在应用程序中的格式获取对象,但现在更难查询特定的日期或月份。而且清除特定日期或月份的旧数据并不容易。

在实践中这样做的好方法是什么? 我的任何一种方法都很好,还是我应该完全做其他事情? 如果一个比它好,我该如何解决我的缺点?

我的应用程序是用 Kotlin 编写的,那么在 kotlin 中是否有一种简单的方法可以从选项 1 中查询数据库并将其返回到平面列表的 for 中?

【问题讨论】:

    标签: android firebase kotlin firebase-realtime-database


    【解决方案1】:

    这边:

    objects
        - objectA
            - 2020:01:22:01
                - property1 : "Value at 1 o'clock on the 22nd of Jan, 2020"
                - property2 : "Value at 1 o'clock on the 22nd of Jan, 2020"
            - 2020:01:22:02
                - property1 : "Value at 2 o'clock on the 22nd of Jan, 2020"
                - property2 : "Value at 2 o'clock on the 22nd of Jan, 2020"
                        ...
        - objectB
            ...
    

    比选项 1 更好,总是倾向于使用扁平数据结构而不是嵌套数据结构。如果您在检索月份或日期时遇到困难,可以将它们都添加为属性:

    objectA
        2020:01:22:01
            property1 : data
            month : 01
            day : 22
    

    但您也可以使用getKey(),然后使用split 检索月份和日期。

    https://firebase.googleblog.com/2013/04/denormalizing-your-data-is-normal.html?m=1

    【讨论】:

      【解决方案2】:

      我认为最好的方法是使用时间戳来存储,我总是在发生时间和日期依赖的地方使用时间戳

      objects
          - objectA
              - 1579721879
                  - property1 : "Value at 1 o'clock on the 22nd of Jan, 2020"
                  - property2 : "Value at 1 o'clock on the 22nd of Jan, 2020"
              - 1579721964
                  - property1 : "Value at 2 o'clock on the 22nd of Jan, 2020"
                  - property2 : "Value at 2 o'clock on the 22nd of Jan, 2020"
      

      或者像这样

      objects
              - 1579721879 : "Value at 1 o'clock on the 22nd of Jan, 2020"
              - 1579721889 : "Value at 1 o'clock on the 22nd of Jan, 2020"
              - 1579721899 : "Value at 2 o'clock on the 22nd of Jan, 2020"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-19
        • 1970-01-01
        • 1970-01-01
        • 2014-06-16
        • 1970-01-01
        • 2017-03-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多