问题
I have a Collection
in Cloud Firestore that has a certain number of items, let's call it Collection
"X". This number of items will constantly be changing. At any given time, I want to listen for the number of items in this Collection
and create several whereEqualto()
calls on a Query
object that is based on another 'Collection', let's call it "Y":
Query queryStore = FirebaseFirestore.getInstance()
.collection("Y")
.whereEqualTo(USER_ID_LABEL, "item 1 from X")
.whereEqualTo(USER_ID_LABEL, "item 2 from X")
.whereEqualTo(USER_ID_LABEL, "item 3 from X");
//Could be more than 3, could be less than 3, constantly changing
Essentially, the number of whereEqualTo()
will be dynamic.
Is this type of query possible?
回答1:
Yes. whereEqualTo() returns a Query object, which can be used to build up the final query you need. There's no obligation to chain the methods all at once, as you've demonstrated in your question. What you've written is equivalent to this:
Query queryStore = FirebaseFirestore.getInstance().collection("Y")
queryStore = queryStore.whereEqualTo(USER_ID_LABEL, "item 1 from X");
queryStore = queryStore.whereEqualTo(USER_ID_LABEL, "item 2 from X");
queryStore = queryStore.whereEqualTo(USER_ID_LABEL, "item 3 from X");
来源:https://stackoverflow.com/questions/50184958/cloud-firestore-dynamic-querying