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.
Asked
Active
Viewed 2,953 times
1

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 Answers
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
-
i need to use the document id out side the oncomplete function though. I'm not very familiar with the Query object – Blaise Lukwayi N Jan 06 '20 at 17:53
-
You need to use a custom callback. Check **[this](https://stackoverflow.com/questions/48499310/firestore-object-with-inner-object/48500679)** out. – Alex Mamo Jan 06 '20 at 17:55
-
th callback example is a void. i want to store the data in a string. any examples? – Blaise Lukwayi N Jan 06 '20 at 21:42
-
There is no way you can do that. You should use that in an asynchronous manner as intended. – Alex Mamo Jan 06 '20 at 22:12