首页 > 解决方案 > 在 Firestore 中查询集合中的不同字段

问题描述

您好,我对 Firestore 和 android studio 还很陌生。我正在尝试查询文档中的两个不同字段,以便在 Firestore 的集合中获取该文档。在这种情况下,集合名称是Dispatch_Records,并且集合中的文档包含一个自动 ID。文档中的两个字段是driver_identityextra_driver_identity。如果当前用户 ID 等于该文档中 driver_identityextra_driver_identity 字段中的用户 ID,则查询应获取该文档。我注意到 Firestore 没有 OR 运算符。我该怎么做才能获取所需的文件?下面是代码:

 String user_id = firebaseAuth.getCurrentUser().getUid();

 firebaseFirestore.collection("Dispatch_Records").whereEqualTo("driver_identity",user_id)
                .whereEqualTo("extra_driver_identity",user_id)
                .addSnapshotListener(Deployments.this,new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

                if(!queryDocumentSnapshots.isEmpty()){

                    for(DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()){

                        if(doc.getType() == DocumentChange.Type.ADDED){


                            String dispatchId = doc.getDocument().getId();
                            DeploymentsConstructor deployment = doc.getDocument().toObject(DeploymentsConstructor.class).withId(dispatchId);
                            deploymentsList.add(deployment);
                            deploymentRecyclerAdapter.notifyDataSetChanged();


                        }

                    }




                }

            }
        });

标签: javaandroidfirebasegoogle-cloud-firestore

解决方案


Firestore 通过 IN 运算符支持 OR 运算符(选中)。在 Java API 中,这是whereIn方法。请在此处查看参考。

祝你好运!


推荐阅读