SMGhost
SMGhost

Reputation: 4037

Firestore relational data model

I'm learning to use Cloud Firestore on Android and so far I'm unable to find answers to some of the questions.

  1. Is it possible to retrieve a list of documents by specifying a list of id's or references without doing separate queries for each id? So far it seems it's not possible.
  2. Is it possible to include reference object from document field when querying document without doing a second query for that reference object? It seems you can't at the moment.
  3. I have a shop root collection and a separate root collection for items in that shop. I want to be able to sort shops by specified item for where it is cheapest. But I have a problem, since if I sort items by price and I get like 1000 results, I need to do 1000 queries to retrieve actual shops which contain those items. How would I optimize this?

I wasn't sure if I should create 3 separate SO questions for this, so I just put them in one. Let me know if this is wrong approach.

Upvotes: 2

Views: 607

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 1

  1. Is it possible to retrieve a list of documents by specifying a list of id's or references without doing separate queries for each id? So far it seems it's not possible.

Yes it is possible. Tasks's whenAllSuccess method (and other whenAll methods) will deliver the results from the tasks to the callback in a List in the order they were passed to whenAllSuccess() method. So as you see in the official documentation, you can pass as an argument a Collection<? extends Task<?>>. Here is an example of doing that using references.

  1. Is it possible to include reference object from document field when querying document without doing a second query for that reference object? It seems you can't at the moment.

Yes it is possible. The answer from the above example, this is what it does. If you want to use separate queries, you can also take a look at another approach here. You'll also get a list.

  1. I have a shop root collection and a separate root collection for items in that shop. I want to be able to sort shops by specified item for where it is cheapest. But I have a problem, since if I sort items by price and I get like 1000 results, I need to do 1000 queries to retrieve actual shops which contain those items. How would I optimize this?

Queries in Firestore are shallow: they only get items from the collection that the query is run against. There is no way to get documents from a top-level collection and other collections or subcollections in a single query. Firestore doesn't support queries across different collections in one step. A single query may only use properties of documents in a single collection. So to solve this, you need to add to each product a new property, which should actually be the id of the store. To get all the store with the cheapest products, you should query the products collection, get the cheapest items and then query again to get the data of the shop that the product is appart of.

Edit:

In general, people over-estimate the amount of use they'll get. So again (in general) I recommend that you don't worry about optimization untill you actually needed it. However, regarding #1, yes it does a bunch of queries but this is how it can be done in Firestore. But don't worry, Firestore works wery well in this kind of situation. Regarding #2 and #3, no, it does not perform query concatenation however it also work very quick even if you are using two queris. There is nothing wrong with nested listeners.

Upvotes: 2

Related Questions