Axil
Axil

Reputation: 3311

Firestore - How to model and query relation of 2 collections - IOT use case

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.

Upvotes: 0

Views: 190

Answers (1)

Thingamajig
Thingamajig

Reputation: 4465

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.

Upvotes: 1

Related Questions