Firestore - 如何建模和查询 2 collections 的关系 - IOT 用例

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

是否可以在 firestore 中建立这种意义上的关系? 我想将 collection 中的一个字段关联到另一个 collection

中的另一个字段

例如:我有 2 个不同的 collection - 跟踪和场地

跟踪 <-- 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

地点 <-- 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

我想查询文档xyz123 并获取该行中的场地名称。 所以输出将是:

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

这是数据的截图: 跟踪数据的原因,在实时用例中,没有奢侈(时间)查询场地 collection 中的名称,因此插入(写入)必须以这种方式(意味着只有 device_unique_identifier可用于插入)。因此,要建立关系,我们只会在查询中进行。

我想建议如何建模和查询这种关系。

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

我更喜欢的技术是将您需要的数据存储在您需要的任何地方。例如。如果您只需要获取跟踪数据并且同时获取场地数据就太过分了,那么只需存储一些带有跟踪的额外数据(在本例中添加名称)并且您不必担心进行多次调用,数据已经存在于您需要的地方。