Firestore-如何建模和查询2个集合的关系-IOT用例 [英] Firestore - How to model and query relation of 2 collections - IOT use case

查看:56
本文介绍了Firestore-如何建模和查询2个集合的关系-IOT用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在救火室中可以做到这种意义上的联系吗? 因此我想将集合中的一个字段与另一个集合中的另一个字段相关联

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

例如:我有2个不同的收藏集-跟踪和地点

Eg: I have 2 different collection - tracking and venue

跟踪<-集合

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

场地<-收藏

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

我想查询文档xyz123并获取该行中场地的名称. 因此输出为:

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"

以下是屏幕截图,显示了数据的外观: 在实时用例中,跟踪数据的原因没有时间(时间)来查询场所集合中的名称,因此插入(写入)必须采用这种方式(意味着只有device_unique_identifier可用于插入).因此,要做这种关系,我们只会在查询中进行.

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.

推荐答案

Firebase中没有JOIN语句的概念.只要数据存在于多个文档中,您就需要调用每个文档并整理数据.

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.

这篇关于Firestore-如何建模和查询2个集合的关系-IOT用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆