0

Would it be possible to do relation in this sense in firestore ? whereby I want to relate a field in collection to another field in another collection

Eg: I have 2 different collection - tracking and venue

tracking <-- collection

1. document(xyz123)
device_unique_identifier = "abcd1234"
timestamp = 10/09/2019 10:00

2. document(xyz567)
device_unique_identifier = "efgh3456"
timestamp = 10/09/2019 11:00

venue <-- collection

1. document(zyx123)
name = "room A"
device_unique_identifier = "abcd1234" <-- this is unique name

2. document(zyx345)
name = "room B"
device_unique_identifier = "efgh3456" <-- this is unique name

I would like to query document xyz123 and get the name of the venue in the row. So the output would be:

document(xyz123)
device_unique_identifier = "abcd1234"
timestamp = 10/09/2019 10:00
venue.name = "room A"

Here is a screenshot how the data may look like: tracking place Reason for tracking data, in a realtime use case, doesnt have the luxury (time) to query the name in venue collection, so insertion (writing) have to be in this way (meaning only the device_unique_identifier is available for insertion). Therefore, to do the relation, we would only do it in the query.

I would like advise how to model and query such a relation.

Axil
  • 3,606
  • 10
  • 62
  • 136

1 Answers1

1

There's no concept of a JOIN statement in Firebase. So long as the data lives in multiple documents, you'll need to call each document and collate the data on your end.

The technique that I prefer is to store the data you'll need wherever you'll need it. E.g. if you need to only grab tracking data and it would be overkill to also grab venue data, then store only a bit extra data with tracking (in this case add the name) and you don't have to worry about making multiple calls, the data will already exist where you need it.

Thingamajig
  • 4,107
  • 7
  • 33
  • 61
  • i understand firestore has offline feature and sync. I'ved not explored that. i thought about that having the venue name to prevent overkill. can firestore store a small db on the mobile ? (in this case venue). that way on tracking data before posting, it can query internal venue database and get the name. is that something that is rational solution (i dont want to create sqlite if possible for this in the mobile, there is something firestore or nice solution to do this) https://firebase.google.com/docs/firestore/manage-data/enable-offline – Axil Sep 20 '19 at 08:46
  • I wouldn't concern yourself with storing the data locally just yet, just make the calls when you need the data. It's extremely fast, if you're making one call, you likely won't notice the difference of it making 2 or 10. – Thingamajig Sep 20 '19 at 13:52
  • well, i still want to figure a way if its supported. by design, if its supported, i'd like to know how to use it – Axil Sep 20 '19 at 14:22
  • I just saw this video https://www.youtube.com/watch?v=jm66TSlVtcc skip to 6:07, could you see if you'ved got an opinion on my subsequent question https://stackoverflow.com/questions/58036882/firestore-how-to-model-and-query-relation-of-2-collections-iot-use-case-v2 – Axil Sep 21 '19 at 02:49
  • You should post another question with your new question. Happy to answer with full context/if I feel I know the answer – Thingamajig Sep 21 '19 at 02:53