我可以设置多个.whereEqualTo指向Firestore查询中的文档中的字段吗?

时间:2019-12-26 07:31:50

标签: android firebase google-cloud-firestore

我有一个文档,其中有city作为字段。我想查询具有

的文档

city == "Tokyo”和city == "Osaka"

该怎么做?

我尝试过这个:

query = ref
            .whereEqualTo(FIRESTORE_EVENT_FIELD_CITY,"Tokyo")
            .whereEqualTo(FIRESTORE_EVENT_FIELD_CITY,"Osaka")
            .whereEqualTo(FIRESTORE_EVENT_FIELD_IS_ACTIVE,true)
            .whereEqualTo(FIRESTORE_EVENT_FIELD_HAS_BEEN_APPROVED,false)
            .whereGreaterThanOrEqualTo(FIRESTORE_EVENT_FIELD_DATE_START_TIME,now.toDate())

          .whereLessThanOrEqualTo(FIRESTORE_EVENT_FIELD_DATE_START_TIME,oneMonthFromNow.toDate())
            .orderBy(FIRESTORE_EVENT_FIELD_DATE_START_TIME, Query.Direction.ASCENDING)
            .limit(limit)

但是我没有收到任何文档,如果我只设置一个指向 一个像.whereEqualTo(FIRESTORE_EVENT_FIELD_CITY,"Tokyo")这样的城市,它就可以了,但是它只给我所有在城市领域中拥有东京的文件

3 个答案:

答案 0 :(得分:2)

从文档中

  

您只能在单个字段上执行范围比较(<,<=,>,> =),并且在复合查询中最多可以包含一个array-contains或array-contains-any子句

https://firebase.google.com/docs/firestore/query-data/queries

citiesRef.whereGreaterThanOrEqualTo("state", "CA")
        .whereLessThanOrEqualTo("state", "IN");
citiesRef.whereEqualTo("state", "CA")
        .whereGreaterThan("population", 1000000);

答案 1 :(得分:0)

您可以检索所有Field数据,然后根据其设置条件,这将有助于您检索值。

ref.whereEqualTo(FIRESTORE_EVENT_FIELD_IS_ACTIVE,true)
                .whereEqualTo(FIRESTORE_EVENT_FIELD_HAS_BEEN_APPROVED,false)
                .whereGreaterThanOrEqualTo(FIRESTORE_EVENT_FIELD_DATE_START_TIME,now.toDate())

                .whereLessThanOrEqualTo(FIRESTORE_EVENT_FIELD_DATE_START_TIME,oneMonthFromNow.toDate())
                .orderBy(FIRESTORE_EVENT_FIELD_DATE_START_TIME, Query.Direction.ASCENDING)
                .limit(limit)
                .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                String city = document.get("city").toString();
                                if (city.equals("Tokyo")){

                                }else if (city.equals("Osaka")){

                                }
                            }
                            Log.d(TAG, list.toString());
                        } else {
                            Log.d(TAG, "Error getting documents: ", task.getException());
                        }
                    }
                });

答案 2 :(得分:0)

我猜你是说worker的意思是user。 Firestore最近发布了city == "Tokyo" and city == "Osaka"逻辑

OR

在当前查询中,您没有收到任何文档,因为前两个OR是互斥的