0

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 ?

Axil
  • 3,606
  • 10
  • 62
  • 136
  • Possible duplicate of [Firestore - How to model and query relation of 2 collections - IOT use case](https://stackoverflow.com/questions/57994090/firestore-how-to-model-and-query-relation-of-2-collections-iot-use-case) – Thingamajig Sep 21 '19 at 02:55
  • On Stack Overflow, please limit yourself to a single question per post. Posts with multiple questions could get closed as "too broad". – Doug Stevenson Sep 21 '19 at 06:13

1 Answers1

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.

Thingamajig
  • 4,107
  • 7
  • 33
  • 61
  • how about model it in this way ? https://stackoverflow.com/questions/51292378/how-do-you-insert-a-reference-value-into-firestore. here it adds reference (object) to say the tracking data. that way we can eliminate join but can keep 2 separate collection – Axil Sep 21 '19 at 02:59
  • since its reference, we dont need to worry on say venue name changes. is this now a good way to model for this case ? – Axil Sep 21 '19 at 03:00
  • As long as the data exists in two separate documents, the same answer applies. – Thingamajig Sep 21 '19 at 03:01
  • A reference stored does not create a path to the document, it's simply storing a path for your use when you retrieve it. For example, if I store a path from a `users` document to a `tweets` document, it's only telling me how to get from the user document to tweets, but only once I retrieve the user document. In other words, read calls to Firestore will not take your data and try to walk a graph. Why can't you just store the venue name wherever you think you will need it, even if it's duplicative? – Thingamajig Sep 21 '19 at 03:05
  • because of a possible offline scenario. ok, how about this, would it be possible during the insertion of the tracking data, given the venue data reference, it will add that venue data as a subdocument of the tracking data "on the server side". meaning the client dont need to know the venue data, just the venue reference id (actually its the unique id of the device) – Axil Sep 21 '19 at 03:08
  • I don't think it should be a sub-anything, it can just be a map of the data. A map in Firestore is like an object or dictionary or key/value pair in other languages. You can easily set up an "onCreate" cloud function that says "On every new document in this collection, grab the venueId used and query the venue collection to get relevant info (e.g. name), and then post that data to the newly created document." You can get started with that here: https://firebase.google.com/docs/functions – Thingamajig Sep 21 '19 at 03:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/199753/discussion-between-axil-and-dan-fein). – Axil Sep 21 '19 at 03:15