Firestore-查询子集合

时间:2020-05-31 22:24:47

标签: java android firebase google-cloud-firestore nosql

enter image description here

enter image description here enter image description here enter image description here

firestore.collection("CATEGORIES").document("HOME").collection("TOP_DEALS")
                .orderBy("index").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                Log.d(TAG, "onComplete: Task Successful? "+task.isSuccessful());
                if (task.isSuccessful()){
                    Log.d(TAG, "onComplete: task.getResult().getDocuments().size() :"+task.getResult().getDocuments().size());
                    for (QueryDocumentSnapshot documentSnapshot:task.getResult()){
                        Log.d(TAG, "onComplete: inside For loop");
                    }
                }else {
                    String error=task.getException().getMessage();
                    Toast.makeText(getContext(), error, Toast.LENGTH_SHORT).show();
                }
            }
        });

enter image description here

我想使用TOP_DEALS子集合中的所有文档。 即使任务成功执行,task.result()也为空,并且每个任务中的语句都不会执行。 我应该如何查询子集合中的所有文档?

1 个答案:

答案 0 :(得分:0)

欢迎光临!

如果我正确理解index文档的字段中的HOME字段。根据{{​​3}}:

orderBy()子句还过滤给定字段的存在。 结果集将不包含不包含以下内容的文档: 给定的字段。

在提供的代码中,TOP_DEALS集合上使用了'orderBy',而字段位于父文档HOME中。

我不确定您要在这里订购什么,但是我认为有两种解决方法。首先,您必须将orderby移到CATEGORIES集合中,或者第二次将索引添加到TOP_DEALS集合中的文档中。