Cloud Firestore - 动态查询

时间:2018-05-05 01:51:20

标签: java android firebase google-cloud-firestore

我在Cloud Firestore中有一个Collection,它有一定数量的项目,我们称之为Collection" X"。这些项目将不断变化。在任何时候,我都想听取Collection中的项目数量,并在基于另一个' Collection&#39的whereEqualto()对象上创建多个Query来电,让我们称之为" 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

基本上,whereEqualTo()的数量将是动态的。

这种查询是否可行?

1 个答案:

答案 0 :(得分:4)

是。 whereEqualTo()返回一个Query对象,可用于构建您需要的最终查询。正如您在问题中所证明的那样,没有义务将这些方法一次性链接起来。你写的东西等同于:

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");