如何使用文档的ID作为游标来遍历集合服务器端(即使用Firebase Java Admin SDK)?
按(随机)ID的顺序迭代文档是可以的(实际的顺序现在不重要),我只想迭代每个文档。希望避免浪费一个字段来将ID存储在文档本身中。
我尝试了以下代码:
String lastId = null;
do {
Query q = collectionRef.limit(100);
if (null != lastId) {
q = q.startAfter(lastId);
}
snapshots = q.get().get().getDocuments();
if (Lists.isNotEmpty(snapshots)) {
lastId = snapshots.get(snapshots.size() - 1).getId();
}
} while (Lists.isNotEmpty(snapshots));
Lists.isNotEmpty(List<?>)
是一种方便的方法,它还检查空引用。
这导致:
java.lang.IllegalStateException: Too many cursor values specified. The specified values must match the orderBy() constraints of the query.
at com.google.common.base.Preconditions.checkState(Preconditions.java:507)
at com.google.cloud.firestore.Query.createCursor(Query.java:339)
at com.google.cloud.firestore.Query.startAfter(Query.java:797)
这意味着我需要一个
orderBy(idFieldName)
查询中的子句。
是否有一个特殊的(保留的)名称(隐藏?)文档的ID属性?
orderBy()
.