英文:
how to get the document id of a document that has a field with a specific value?
问题
我想获取具有特定值的字段的文档的文档 ID,例如在此集合中,我想检索“itemName”为“eggroll”的文档的文档 ID。
英文:
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
得分: 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
英文:
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
得分: 1
你需要执行一个简单的查询,就像这里所提到的那样。
执行它。然后使用document.getId()来获取与查询条件匹配的文档的ID。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论