如何自动为Cloud Firestore创建索引?

Ioa*_* P. 5 android firebase google-cloud-firestore

我有一个集合,Lists其中存储了一堆文档.每个文档都包含一些细节和一个集合users.我users使用uid作为键并将boolean作为值存储在Collection中的用户.这意味着只有那些用户才能阅读该特定列表.这是我的数据库结构.

在此输入图像描述 在我的代码中,我使用此查询:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference listsRef = rootRef.collection("Lists");
Query query = listsRef.orderBy("date").whereEqualTo("users."+uid, true);
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

FAILED_PRECONDITION: The query requires an index. You can create it here: ...
Run Code Online (Sandbox Code Playgroud)

如果我去控制台创建所需的索引,一切正常.但是是否有可能自动创建索引,因为我无法为加入我的应用程序的每个用户手动创建索引.如果我为每个用户提供了一些排序索引,是否会出现问题?

还有另一种解决方法吗?

提前致谢!

Fra*_*len 7

Cloud Firestore会自动在文档的各个字段上创建索引.跨越多个字段的复杂索引(目前)只能在Firebase控制台中手动创建.

如果您发现需要以编程方式创建索引,则通常应考虑扩充数据结构以允许反向查找.例如,在您的场景中,我将添加一个userLists集合,您可以在其中保留每个用户的列表.