Cloud Firestore - Dynamic Querying

独自空忆成欢 提交于 2021-02-19 01:32:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!