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

时光毁灭记忆、已成空白 提交于 2019-12-11 15:35:39

问题


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: 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.


回答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.



来源:https://stackoverflow.com/questions/57994090/firestore-how-to-model-and-query-relation-of-2-collections-iot-use-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!