我如何修复抱怨 order by on equality 过滤器的 firestore 错误

How to i fix a firestore error complaining about order by on equality filter

这个有效

collection
    .where('startDate', '>=', startDate)
    .get();

这会导致错误

collection
    .where('startDate', '>=', startDate)
    .where('startDate', '<=', endDate)
    .get();

错误 Error: order by clause cannot contain a field with an equality filter startDate

我不明白错误或如何修复它。我在 firestore 中的数据类型是日期。该文档允许范围查询,只要它是相同的字段,所以我不确定为什么这不起作用。

事实证明 startDate / endDate 变量是一个整数,一旦我将其更改为日期对象,查询就会开始工作。

如果 startDate 和 endDate 相同,您也会得到该错误,例如:

    let startDate = new Date('1980-12-01')
    let endDate = new Date('1980-12-01')

解决方案是:

    let startDate = new Date('1980-12-01 00:00:00')
    let endDate = new Date('1980-12-01 23:59:59')

我遇到了同样的错误,因为我在两个 where 子句中使用了完全相同的字符串

.where("field", ">=", "somestring").where("field", "<=", "somestring")

我的代码是:

let boxSearch = document.getElementById("boxSearch").value;

dbRef.collection("clients").where("name", ">=", boxSearch).where("name", "<=", boxSearch).get()

我的解决方案是在第二个 where 子句上使用 "somestring" + "~",如下所示:

dbRef.collection("clients").where("name", ">=", boxSearch).where("name", "<=", boxSearch + "~").get()

dbRef.collection("clients").orderBy("name").startAt(boxSearch).endAt(boxSearch + "~").get()

如果订购依据的相同字段 equal.it 不应该是布尔值 field.actually 则没有必要 right.because 如果您添加等号,您会得到 true 或 false..那是为什么会报错..

例如:

Firestore firestore_db = getFirestoreInstance();
Query query=firestore_db.collection("collection").orderBy("isAllow",Direction.DESCENDING).whereEqualTo("isAllow", false);

如果您的查询如上。它不会工作..