how to get the document id of a document that has a field with a specific value?

谁说胖子不能爱 提交于 2020-01-15 10:14:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!