【发布时间】:2020-03-04 08:24:32
【问题描述】:
我想从我的收藏中加载保存在两个时间戳之间的所有文档。我在我的 component.ts 中调用了一个服务函数,我在其中将我需要的 2 个时间戳设置为参数。我如何查询数据库以便使用 ref.where 返回我想要的文档
此功能位于我的服务中
findItems(colId: string, timestamp1: Date, timestamp2: Date){
return this.db.collection(`items/${colId}/stuff`,ref =>{
return ref.where("createdAt", ">=", startTime );
}).valueChanges()
}
我在上面的组件中调用了这个函数
public items: Observable<any>
this.myService.findItems(this.item.colId, this.item.time1, this.item.time2)
一项的实际字段:
开始/结束时间戳示例:
【问题讨论】:
-
将 where 子句与范围 on the same field 结合起来应该不会有任何问题。但是,您需要使用
createdAt字段创建数据,因为默认情况下 Firestore 不会为其文档添加时间戳。您的代码中startTime的来源也不清楚。 -
我会尝试 ref.where("createdAt",">=",timestamp1).where("createdAt","
-
如果它不起作用(我注意到您现在查询的字段名称是
created而不是createdAt,请显示您数据库中的一些数据)。您也可能无法直接与 Date 对象进行比较。 -
我检查了时间戳,因为我将它们转换为 Firestore 格式的时间戳。我通过数据库界面手动添加了一些,从我的应用程序中添加了一些,并且外观相同。我将在我的帖子中提供一些照片
-
不清楚这些图像是什么,甚至字段名称应该是什么(现在也有带有
startTime和endTime的字段。请显示更多上下文。
标签: angular firebase google-cloud-firestore timestamp angularfire