Skip to content
Advertisement

no FireBase FireStore , is it possible to read only until you find a specific document?

  • I have an app that a user registers animals,
  • these animals are separated by lot,
  • so, to know where the animal is, the user just types his TAG and I look for the tag in all lots of my database,
  • example:

Collection> Lots

Documents: Lot+NumberLot (Lot+number is the ID, for example: Lot01, Lot02).

Within each Document Lot+NumberLot I have a subcollection “Animals”, within each subCollection animals,

I have documents for each animal, where the ID is the TAG that the user types.

What I do is as follows, the user types the TAG but he does not know the lot, so, I search for that TAG in all lots, however, if the user has many lots and wants to search only 1 animal, I will be charged for all lots even if it is to retrieve a single document from “Animals”, I am currently doing this:

-My Code

JavaScript

This program finds the animal if it exists, but I’m always reading all the documents and all the lots, I wanted to make sure that after I found the animal, I would not be charged any more, if I use Where will I still be charged for all documents?

Firestore image example: Database

For example, I want to find the 041712 TAG, but I don’t know the lot it is in (since the user will not remember the lot, only the Tag), I didn’t want to have to go through all the batches and tags to find it, I wanted that after finding him, I don’t need to read the rest so I would not consume unnecessary queries

Advertisement

Answer

You have an animal object that has as the id of the document 041712. If this id is also contained as a property within that document, to get all the animals without knowing the lot, a collection group query is required. In code, it should look similar to this:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement