为什么我无法从我的Firestore数据库中获取集合中的所有文档?

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

Why can't i get all the document from a collection in my Firestore Database?

问题

尝试从Firestore获取文档时,即使我的集合中有20个文档,我只能获取到8个。使用下面的代码,仅显示了8条日志消息:

mFStore.collection("In")
        .orderBy("Time of submit", Query.Direction.ASCENDING)
        .whereGreaterThanOrEqualTo("Time of submit", startDateTimeStamp)
        .whereLessThanOrEqualTo("Time of submit", endDateTimeStamp)
        .get()
        .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                List<DocumentSnapshot> snapshotList = queryDocumentSnapshots.getDocuments();
                for (DocumentSnapshot snapshot : snapshotList) {
                    Timestamp timestamp = (Timestamp) snapshot.getData().get("Time of submit");
                    Date date = timestamp.toDate();
                    String day = format.format(date);
                    String strQty = snapshot.get("Quantity").toString();
                    Log.d(TAG, day + " ," + strQty);
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d(TAG, e.getMessage());
            }
        });

使用下面的代码,我尝试删除上述的3个查询条件,但仍然只能获取到15条日志消息:

mFStore.collection("In")
        .get()
        .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public onSuccess(QuerySnapshot queryDocumentSnapshots) {
                List<DocumentSnapshot> snapshotList = queryDocumentSnapshots.getDocuments();
                for (DocumentSnapshot snapshot : snapshotList) {
                    Timestamp timestamp = (Timestamp) snapshot.getData().get("Time of submit");
                    Date date = timestamp.toDate();
                    String day = format.format(date);
                    String strQty = snapshot.get("Quantity").toString();
                    Log.d(TAG, day + " ," + strQty);
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d(TAG, e.getMessage());
            }
        });

这是我的数据库结构的图片:

my_database_structure

有人能告诉我是否实际上获取了所有文档,但某种方式没有获取到所有的日志,还是我只获取到了部分文档?

英文:

I was trying to get documents from Firestore with query but even though my collection has 20 documents I can only get 8.
With the code below only 8 log messages was shown:

mFStore.collection(&quot;In&quot;)
        .orderBy(&quot;Time of submit&quot;, Query.Direction.ASCENDING)
        .whereGreaterThanOrEqualTo(&quot;Time of submit&quot;,startDateTimeStamp)
        .whereLessThanOrEqualTo(&quot;Time of submit&quot;,endDateTimeStamp)
        .get()
        .addOnSuccessListener(new OnSuccessListener&lt;QuerySnapshot&gt;() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                List&lt;DocumentSnapshot&gt; snapshotList = queryDocumentSnapshots.getDocuments();
                for(DocumentSnapshot snapshot: snapshotList){
                    Timestamp timestamp = (Timestamp) snapshot.getData().get(&quot;Time of submit&quot;);
                    Date date = timestamp.toDate();
                    String day = format.format(date);
                    String strQty = snapshot.get(&quot;Quantity&quot;).toString();
                    Log.d(TAG ,day + &quot; ,&quot; + strQty);
                }

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d(TAG ,e.getMessage());
            }
        });

With the code below, I've tried removing all the 3 queries from above but I still was only getting 15 log messages.

mFStore.collection(&quot;In&quot;)
        .get()
        .addOnSuccessListener(new OnSuccessListener&lt;QuerySnapshot&gt;() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                List&lt;DocumentSnapshot&gt; snapshotList = queryDocumentSnapshots.getDocuments();
                for(DocumentSnapshot snapshot: snapshotList){
                    Timestamp timestamp = (Timestamp) snapshot.getData().get(&quot;Time of submit&quot;);
                    Date date = timestamp.toDate();
                    String day = format.format(date);
                    String strQty = snapshot.get(&quot;Quantity&quot;).toString();
                    Log.d(TAG ,day + &quot; ,&quot; + strQty);
                }

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d(TAG ,e.getMessage());
            }
        });

Here is a picture of my database:
my_database_structure

Can someone tell me if I'm actually getting all the document but somehow not getting all the logs or am I just not getting all my documents?

答案1

得分: 0

我不知道我需要为这些查询添加indexes。在我添加它们之后,我成功获取了所有我想要的文档。

英文:

I didn't know I needed to add indexes for the queries. After I added them, I managed to get all the documents I wanted.

huangapple
  • 本文由 发表于 2023年2月24日 00:47:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547842.html
匿名

发表评论

匿名网友

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

确定