0
votes

I have a user collection. Each user has a list of collections with different names, ie "activities", "time", "distance", "sensor-a", "sensor-b", etc. The id of each document in these collections is common. So for example the all the documents with the same id are associated with each other.

Each document in the "activities" collection has properties that I would search on, while "time", "distance", and the "sensor-x" documents are for the most part a list of data points for various sensors. So for example the first element in each list correspond to each other, ie sensor-a[42] is the sensor reading at time[42], distance[42] and sensor-b[42].

So the data looks something like this:

user/100/activites/200
user/100/distance/200
user/100/time/200
user/100/sensor-a/200
user/100/sensor-b/200

user/100/activites/201
user/100/distance/201
user/100/time/201
user/100/sensor-a/201
user/100/sensor-b/201

I would like to be able to filter out a list of "activities" based on some property of the activity and then get all the documents from the other collections that have the same id as those I filtered.

I know I can create a list of ids I am interested in and then simply get each document one at a time but that does not seem very efficient.

I know that Firestore supports an in query method but that seems to be limited to a maximum of 10 entries, ie my filtered list of activities would need to be 10 or less.

I also see that Firestore has collection groups but they seem to require that all the collections have the same name. I think I could use this but I think I would need to restructure my data, probably adding a level of hierarchy, such as "activities", "distance", and "time" each have a data document that contains the current collections.

Is there any efficient way I can do this query with restructuring my data?

1

1 Answers

2
votes

A single Firestore read operation can only read documents from a single collection, or from all collections with the same name. There is no way to otherwise read from multiple collections. The typically workaround is to perform a separate read for each collection, and then merge the results in your application code.

But here I would consider storing all the activity types for a single user in a single subcollection, and giving them a activityType field. One of the unique benefits of using Firestore is the performance of a query does not depend on the number of documents that it needs to consider, so there really is no penalty for combining the activities in a single subcollection.