问题
extending to my question Firestore - How to model and query relation of 2 collections - IOT use case
I'ved now seen a video on this, and this recommends modelling relations using document ID. https://www.youtube.com/watch?v=jm66TSlVtcc skip to 6:07
I want to know if it would work in this case (modifying the example from my original question to fit to this youtube recommendation of firestore relation:
Eg: I have 2 different collection - tracking and venue
tracking <-- collection
1. document(xyz123)
venueId = "abcd1234"
timestamp = 10/09/2019 10:00
2. document(xyz567)
venueId = "efgh3456"
timestamp = 10/09/2019 11:00
venue <-- collection
1. document(abcd1234) <-- notice i shift the device_unique_identifier here instead
name = "room A"
// device_unique_identifier = "abcd1234" <-- this is unique name
2. document(efgh3456) <-- notice i shift the device_unique_identifier here instead
name = "room B"
// device_unique_identifier = "efgh3456" <-- this is unique name
Main question: 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"
On another possible extra question, when inserting the tracking data, would it be possible to auto insert the venue data as an object in firestore backend without the need to query venue the data ?
回答1:
This is not possible with Firebase. There's no concept of JOIN in Firebase. If the data lives in two separate documents you'll need to either reconsider how you're storing the data (so that the data can be called all at once, etc.) or make all the calls needed to achieve the end desired output.
You can definitely store the data like you're doing, but you can't query one document to get the output with the current structure.
来源:https://stackoverflow.com/questions/58036882/firestore-how-to-model-and-query-relation-of-2-collections-iot-use-case-v2