首页 > 解决方案 > Cloud Firestore - 动态查询

问题描述

Collection在 Cloud Firestore 中有一个有一定数量的项目,我们称之为Collection“X”。这个项目的数量将不断变化。在任何给定时间,我都想监听其中的项目数量,并在基于另一个“集合”的对象上Collection创建多个whereEqualto()调用,我们称其为“Y”:Query

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()将是动态的。

这种查询可能吗?

标签: javaandroidfirebasegoogle-cloud-firestore

解决方案


是的。 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");

推荐阅读