简体   繁体   English

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

[英]can I set more than one .whereEqualTo pointing to a field in a document in a firestore query?

I have a document that has city as a field.我有一个以city为字段的文档。 I want to query a document which has我想查询一个具有

city == "Tokyo " and city == "Osaka" . city == "Tokyo ”和city == "Osaka"

how to do that ?怎么做 ?

I have tried this one:我试过这个:

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)

but I don't receive any document, If I only set one query pointing to a city, like this only .whereEqualTo(FIRESTORE_EVENT_FIELD_CITY,"Tokyo") , then it works, but it only give me all documents that has tokyo in the city field但我没有收到任何文件,如果我只设置一个指向一个城市的查询,就像这样 only .whereEqualTo(FIRESTORE_EVENT_FIELD_CITY,"Tokyo") ,那么它可以工作,但它只给我所有在城市中有东京的文件场地

From the docs:从文档:

You can only perform range comparisons (<, <=, >, >=) on a single field, and you can include at most one array-contains or array-contains-any clause in a compound query您只能对单个字段执行范围比较(<、<=、>、>=),并且您最多可以在复合查询中包含一个 array-contains 或 array-contains-any 子句

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

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

I am guessing when you say you want city == "Tokyo" and city == "Osaka" you meant OR .我猜当你说你想要city == "Tokyo" and city == "Osaka"你的意思是OR Firestore recently released OR logic Firestore 最近发布的OR逻辑

query = ref
        .whereIn(FIRESTORE_EVENT_FIELD_CITY,Arrays.asList("Tokyo", "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)

In your current query, you are not receiving any document because your first two where are mutually exclusive在您当前的查询中,您没有收到任何文件,因为您的前两个where是互斥的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能在查询 Firestore 中使用 whereEqualTo 条件 - Why I can't use the whereEqualTo condition in query Firestore Firestore查询Android WhereEqualTo - Firestore Query Android WhereEqualTo 我可以在一个属性中添加多个字段吗? - Can I add more than one field to a single attribute 如何在 Android 中使用 AlarmManager 设置多个警报? - How can I set more than one alarm with AlarmManager in Android? 可以使用表示Firestore文档中字段的字符串列表构造查询吗? - Can a query be constructed using a list of strings that represent a field in a firestore document? 在多个文档上进行Firestore交易是否比逐个更新每个文档更有效? - Are Firestore transactions on multiple documents more efficient than updating each document one by one? Sort Entry Set of String,Integer by more one field - Sort Entry Set of String, Integer by more than one field 如何从 FireStore 查询文档名称? - How can I query for a document name from FireStore? 如何在 queryDocumentSnapshot Firestore 中查询文档字段 - How to query a document field inside of queryDocumentSnapshot Firestore 我们是否可以避免将所有字段映射到 springdata 中的实体类以进行弹性搜索,因为我在 json 文档中有 100 多个字段? - Can we avoid mapping all the fields to entity class in springdata for elasticsearch, as I have more than 100 field in the json document?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM