问题
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.
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/59614198/how-to-get-the-document-id-of-a-document-that-has-a-field-with-a-specific-value