[英]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.