如何获取具有特定值字段的文档的文档ID?

huangapple go评论74阅读模式
英文:

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.如何获取具有特定值字段的文档的文档ID?

答案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(&quot;delivery&quot;);
Query nameQuery = deliveryRef.whereEqualTo(&quot;itemName&quot;, &quot;eggroll&quot;);
nameQuery.get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
    @Override
    public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; 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.

huangapple
  • 本文由 发表于 2020年1月6日 22:53:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定