0

I have some trouble with the docs of firestore and their best practices in order to query subcollections and the parent document. I trying to query inside my firebase function with Typescript.

The DB:

├── User (Collection)
│ └── User (Document with ID)
│ │ └── Plan (Subcollection)

I have a list of user IDs in a array which I need to collect information from the user document and from the subcollections plan the current document.

.where("id", in, idArray).get()

But how tho get the Data from the User document and the subcollection (Plan) document (current) I don't know the most efficient way to query in the scenario and the documentation is not really clear for me.

Thanks

Solesins
  • 11
  • 2
  • 7
  • 1
    Can you post a screenshot of your Firestore Document Structures? It's not clear where the array is. – Dharmaraj Apr 18 '21 at 13:39
  • @Dharmaraj the array has nothing to do with the firestore itself. It contains only the ids of the wanted user docs – Solesins Apr 18 '21 at 13:51

1 Answers1

0

A Firestore query can only consider the information in the documents that it'll return. A query on the Plan subcollection has no way to access/query on information in the User documents.

So if you want to select plans for a set of users, you'll need to include the ID of the user document into each plan document too. This sort of duplication is quite common when working with NoSQL databases, and is one of the reasons Firestore can meet its performance guarantees.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807