首页 > 解决方案 > Firestore 中的复合查询 - “order by”和“where”中的不同字段

问题描述

这可以在 Firebase Firestore 中使用吗?

var result = await firestoreInstance
  .collection('messages')
  .where('place', whereIn: ["value1","value2","value3","value4","value5","value6","value7","value8","value9"])
  .orderBy('dateAdded')  
  .startAfterDocument(lastDocument)  
  .limit(documentLimit)  
  .getDocuments();

我的意思是,我想根据一个字段('place')上的 IN 条件过滤数据,然后根据另一个字段('dateAdded')对其进行分页。

它会在“place”和“dateAdded”字段上使用复合索引吗?我 Firestore 允许它,它可以在 Flutter SDK 中工作吗?

标签: firebasefluttergoogle-cloud-firestore

解决方案


应该可以正常工作 - 我已经在我的代码中执行了类似的操作。我通过尝试发现的一件事 - 使用“EndBeforeDocument”调用进行分页似乎需要反转orderBy,因此它需要设置多个复合索引。您创建这些索引的页面链接将在 .get() 调用的“错误”中返回(因此请适当设置回调以捕获此问题)。

所以道格通常是对的,顺便说一句,不尝试(和/或搜索 StackOverflow)就提出问题的不礼貌 - 你很幸运,因为我刚刚为我的应用程序的库编写了自己的分页界面。虽然我打算尽快将接口作为 NPM 包发布,但它可能对您没有帮助 - 我在 React 下使用 Web API (javascript)。


推荐阅读