Reputation: 9506
So I have several objects I am monitoring on my server, and every hour I want to post the readings from these objects to my database. I need an easy way to have these so I can sort them by date in an array, get values from a specific day easily, and clean up old data that is no longer needed from a specific day.
I currently have two ideas of how I might structure my data:
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
...
This method gives me the advantage of easily querying a specific day or month and getting the values from it. It also easily lets me clear old data from a specific day or month.
However the downside is I can't think of an easy way to query it to have the data in my app as a flat list sorted by time (which, for the most part, is how i need to data)
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
...
This model makes it much easier to get the objects in the format I need them to be in the app, however now its harder to query just a specific day or month. And clearing old data from a specific day or month is not easy.
What is a good way of doing this in practice? Are either of my methods good or should I be doing something else entirely? If one is good than how do I solve the downside I have with it?
My app is written in Kotlin so is there an easy way in kotlin to query the database from option 1 and have it return in the for of a flat list?
Upvotes: 0
Views: 266
Reputation: 1725
I think the best way is to store using time stamp, I always use timestamp where time and date dependencies occurs
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"
or like this
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"
Upvotes: 0
Reputation: 80952
This way:
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
...
Is better than option 1, always favor using a flat data structure instead of a nested one. If you have difficulties retrieving the month or the day, you can add them both as a property:
objectA
2020:01:22:01
property1 : data
month : 01
day : 22
But you can also use getKey()
and then use split
to retrieve the month and day.
https://firebase.googleblog.com/2013/04/denormalizing-your-data-is-normal.html?m=1
Upvotes: 1