我有一个Firestore数据库,其中包含一个集合中的文档列表。每个文档都有一个预定的日期,该日期是一种“时间戳”数据类型。我能够按计划的日期获取数据,但无法获取不等于特定计划日期的所有文档。所以我要做的是过滤所有不等于预定日期的数据,但是我必须权衡一下,我必须将所有收集文档存储在前端
以下查询按计划日期获取数据。
where: [
[
'ScheduleDateTime',
'>=',
new Date('2020-02-12 00:00:00'),
],
[
'ScheduleDateTime',
'<=',
new Date('2020-02-12 23:59:59'),
],
],
逻辑上我在前端内置了不相等的日期。
const Quotations =
allDocuments.filter(
ele =>
scheduledDateList.indexOf(
ele.id
) == -1
);
我们已经知道我们不能在Firestore中使用!=
或OR
条件。在消防站工作非常困难。任何建议或解决方案都会有所帮助。
答案 0 :(得分:1)
正如您在问题中提到的那样,并在doc中进行了解释:
Cloud Firestore不支持以下类型的查询:
- …
不支持带有!= 子句的- 查询。在这种情况下,将查询拆分为大于查询和小于查询。 ...
- 逻辑 OR 查询。在这种情况下,您应该为每个OR条件创建一个单独的查询,并将查询结果合并到您的应用中。
以下功能将基于ScheduleDateTime
时间戳字段合并两个查询:
async function getDatesNotEqual() {
const isLess = datesRef
.where(
'ScheduleDateTime',
'<=',
firebase.firestore.Timestamp.fromDate(
new Date('2020-02-12 00:00:00')
)
)
.get();
const isMore = datesRef
.where(
'ScheduleDateTime',
'>',
firebase.firestore.Timestamp.fromDate(
new Date('2020-02-12 23:59:59')
)
)
.get();
const [isLessQuerySnapshot, isMoreQuerySnapshot] = await Promise.all([
isLess,
isMore
]);
const isLessThanDocsArray = isLessQuerySnapshot.docs;
const isMoreThanDocsArray = isMoreQuerySnapshot.docs;
return _.concat(isLessThanDocsArray, isMoreThanDocsArray);
}
//Here we call the async function
getDatesNotEqual().then(result => {
result.forEach(docSnapshot => {
console.log(docSnapshot.data());
});
});
请注意,我们使用Lodash库来合并两个数组(_.concat()
),但是您可以使用其他技术来做到这一点。
还要注意,我们使用Firestore Timestamp
的{{3}}方法来构建查询。