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

lotRef.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                    @Override
                                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if(task.isSuccessful()){
    
for (QueryDocumentSnapshot documentSnapshot: task.getResult()){
CollectionReference aniRef = lotRef.document(documentSnapshot.getId().toString())
                                                        .collection("Animals");
aniRef.get()
                                                        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                                                if(task.isSuccessful()) {
for (DocumentSnapshot documentSnapshot1 : task.getResult()) {
                                                                        Animal aniFounded= documentSnapshot1.toObject(Animal.class);
if (aniFounded.getTagAni() != null && aniFounded.getTagAni().equals(tagAni)) {
                                                                            Toast.makeText(Activity_AttSemBLE.this, aniFounded.getTagBoi().toString(),
                                                                                    Toast.LENGTH_LONG).show();
                                                                            founded = true;
                                                                            break;
    }
      }
     }else{
                                                                    Toast.makeText(Activity_AttSemBLE.this, "Not found", Toast.LENGTH_LONG).show();
    }
     }
        });
if(founded){
    founded = false;
     break;
                       }
                               }
                          }else{
                                            Toast.makeText(Activity_AttSemBLE.this, "No animal found", Toast.LENGTH_LONG).show();
                                        }
                                    }
                                });

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:

db.collectionGroup("Animals").whereEqualTo("id", "041712").limit(1).get()
    .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
        @Override
        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
            // Iterate the queryDocumentSnapshots object
        }
    });
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement