没有 FireBase FireStore,是否有可能仅读取直到找到特定文档为止?

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

no FireBase FireStore , is it possible to read only until you find a specific document?

问题

  • 我有一个应用程序,用户可以注册动物,
  • 这些动物按批次分开,
  • 所以,为了知道动物在哪里,用户只需输入它的标签,然后我在我的数据库中查找该标签在所有批次中的位置,
  • 示例:

> 集合> 批次

文档:批次号+批次编号(批次号+编号是ID,例如:批次01,批次02)。

在每个文档批次号+批次编号内,我有一个子集合 "Animals",在每个子集合 "Animals" 中,

我为每个动物都有一个文档,其中ID是用户输入的标签。

我的做法如下,用户输入标签,但他不知道批次号,所以,我会在所有批次中搜索该标签,然而,如果用户有很多批次,并且只想搜索一个动物,即使只是从 "Animals" 中检索一个文档,我也会为所有批次付费。我目前正在这样做:

-我的代码

lotRef.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if(task.isSuccessful()){
            for (QueryDocumentSnapshot documentSnapshot: task.getResult()){
                CollectionReference aniRef = lotRef.document(documentSnapshot.getId().toString())
                                                    .collection("Animals");
                aniRef.get()
                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            if(task.isSuccessful()) {
                                for (DocumentSnapshot documentSnapshot1 : task.getResult()) {
                                    Animal aniFounded = documentSnapshot1.toObject(Animal.class);
                                    if (aniFounded.getTagAni() != null && aniFounded.getTagAni().equals(tagAni)) {
                                        Toast.makeText(Activity_AttSemBLE.this, aniFounded.getTagBoi().toString(),
                                                        Toast.LENGTH_LONG).show();
                                        founded = true;
                                        break;
                                    }
                                }
                            } else {
                                Toast.makeText(Activity_AttSemBLE.this, "Not found", Toast.LENGTH_LONG).show();
                            }
                        }
                    });
                if(founded){
                    founded = false;
                    break;
                }
            }
        } else {
            Toast.makeText(Activity_AttSemBLE.this, "No animal found", Toast.LENGTH_LONG).show();
        }
    }
});

这个程序如果动物存在则能找到,但我总是在读取所有的文档和批次,我希望确保在找到动物后,不再产生额外的费用,如果我使用 "where" 语句,我仍然会为所有文档付费吗?

Firestore 的示例图像:
(图片已省略)

例如,我想要找到标签为 041712 的动物,但我不知道它在哪个批次(因为用户不会记得批次,只记得标签),我不想必须遍历所有的批次和标签来找到它,我希望在找到它之后,不需要再读取其他内容,以避免不必要的查询消耗。

英文:
  • I have an app that a user registers animals,
  • these animals are separated by lot,
  • so, to know where the animal is, the user just
    types his TAG and I look for the tag in all lots of my database,
  • example:

> Collection> Lots

Documents: Lot+NumberLot (Lot+number is the ID, for example: Lot01, Lot02).

Within each Document Lot+NumberLot I have a subcollection "Animals", within each subCollection animals,

I have documents for each animal, where the ID is the TAG that the user types.

What I do is as follows, the user types the TAG but he does not know the lot, so, I search for that TAG in all lots, however, if the user has many lots and wants to search only 1 animal, I will be charged for all lots even if it is to retrieve a single document from "Animals", I am currently doing this:

-My Code

lotRef.get()
.addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
@Override
public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
if(task.isSuccessful()){
for (QueryDocumentSnapshot documentSnapshot: task.getResult()){
CollectionReference aniRef = lotRef.document(documentSnapshot.getId().toString())
.collection(&quot;Animals&quot;);
aniRef.get()
.addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
@Override
public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
if(task.isSuccessful()) {
for (DocumentSnapshot documentSnapshot1 : task.getResult()) {
Animal aniFounded= documentSnapshot1.toObject(Animal.class);
if (aniFounded.getTagAni() != null &amp;&amp; aniFounded.getTagAni().equals(tagAni)) {
Toast.makeText(Activity_AttSemBLE.this, aniFounded.getTagBoi().toString(),
Toast.LENGTH_LONG).show();
founded = true;
break;
}
}
}else{
Toast.makeText(Activity_AttSemBLE.this, &quot;Not found&quot;, Toast.LENGTH_LONG).show();
}
}
});
if(founded){
founded = false;
break;
}
}
}else{
Toast.makeText(Activity_AttSemBLE.this, &quot;No animal found&quot;, Toast.LENGTH_LONG).show();
}
}
});

This program finds the animal if it exists, but I'm always reading all the documents and all the lots, I wanted to make sure that after I found the animal, I would not be charged any more, if I use Where will I still be charged for all documents?

Firestore image example:
没有 FireBase FireStore,是否有可能仅读取直到找到特定文档为止?

For example, I want to find the 041712 TAG, but I don't know the lot it is in (since the user will not remember the lot, only the Tag), I didn't want to have to go through all the batches and tags to find it, I wanted that after finding him, I don’t need to read the rest so I would not consume unnecessary queries

答案1

得分: 3

你有一个动物对象,其文档的 ID 为 041712。如果这个 ID 也作为文档中的属性存在,要获取所有动物而不知道批次,就需要进行集合组查询。在代码中,应该类似于这样:

db.collectionGroup("Animals").whereEqualTo("id", "041712").limit(1).get()
    .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
        @Override
        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
            // 遍历 queryDocumentSnapshots 对象
        }
    });
英文:

You have an animal object that has as the id of the document 041712. If this id is also contained as a property within that document, to get all the animals without knowing the lot, a collection group query is required. In code, it should look similar to this:

db.collectionGroup(&quot;Animals&quot;).whereEqualTo(&quot;id&quot;, &quot;041712&quot;).limit(1).get()
.addOnSuccessListener(new OnSuccessListener&lt;QuerySnapshot&gt;() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
// Iterate the queryDocumentSnapshots object
}
});

huangapple
  • 本文由 发表于 2020年9月22日 19:52:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64009175.html
匿名

发表评论

匿名网友

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

确定