1

I would like to get the document id of a document which has a field that has a specific value for example in this collection, i would like to retrieve the document id of the document where the "itemName" is "eggroll.firestore collection

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Blaise Lukwayi N
  • 127
  • 1
  • 10
  • This sort of simple query is covered in the documentation, I suggest reading it over if you are going to use Firestore for more queries: https://firebase.google.com/docs/firestore/query-data/queries#simple_queries – Doug Stevenson Jan 06 '20 at 16:04

2 Answers2

2

To solve this, please try the following lines of code:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference deliveryRef = rootRef.collection("delivery");
Query nameQuery = deliveryRef.whereEqualTo("itemName", "eggroll");
nameQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                Log.d(TAG, document.getId());
            }
        }
    }
});

The output in your logcat will be the following document id:

8jy6 ... fcrm
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
1

You need to perform a simple query like the one mentioned here.

Execute it. And the document.getId() which will give you the ID of the document matching the query criteria.

Waelmas
  • 1,894
  • 1
  • 9
  • 19