gpt4 book ai didi

angular - 获取 Firebase Firestore 集合中最后创建的文档

转载 作者:行者123 更新时间:2023-12-03 22:55:25 24 4
gpt4 key购买 nike

有没有办法在 Firebase Firestore 集合中获取上次创建的文档?我的要求是当用户签名时,我必须在名为“history”的集合中添加一个文档;当用户退出时,我想使用名为“退出”的字段更新该文档;因此,如果我获得最后添加的文档,则很容易更新。有没有办法做到这一点?

最佳答案

Is there any way to get the last created document in Firebase Firestore collection?


就在这里!实现此目的的最简单方法是添加 date属性到您集合中的每个对象,然后根据这个新属性降序查询它并调用 limit(1)功能。就是这样!
正如@eyyo 在他的评论中提到的,这是一个具体的例子。假设您的数据库架构如下所示:
Firestore-root
|
--- history (collection)
|
--- docId (document)
|
--- date: June 24, 2020 at 6:46:25 PM UTC+3
|
--- //Other properties
这是必需的查询:
this.historyRef = afs.collection<History>('history', ref => ref.orderBy('date', 'desc').limit(1));
this.history = this.historyRef.snapshotChanges().map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as Hisotory;
const docId = a.payload.doc.id;
return { docId, ...data };
});
});

关于angular - 获取 Firebase Firestore 集合中最后创建的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54178951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com