在 collection 中获取最新文档
Getting the latest document in a collection
我有 collection 件商品。当我听它的时候,我发现我一直在得到所有的东西。我只需要最新的文档,我需要在添加新文档时响应。我试图像这样限制查询:
this.itemsCollection = this.afs.collection('games').doc(gameId)
.collection('hands', ref => ref.orderBy('started').limit(1));
this.items = this.itemsCollection.valueChanges();
this.items.subscribe((hands: Hand[]) => {
console.log('got hand ', hands);
});
遗憾的是,它不起作用。添加新项目时我没有收到任何项目。当我删除限制功能时,我每次都得到所有项目...
最简单的实现方法是为集合中的每个对象添加一个 Date 属性,然后根据这个新的 属性 降序简单地查询它并调用 limit(1) 函数。您可以查看有关如何 "Order and limit data with Cloud Firestore" and "Get data with Cloud Firestore"
的文档
思路如下:
db.collection("YourCollection")
.orderBy('Date', 'desc')
.limit(1).get()
})
如果有帮助请告诉我。
我有 collection 件商品。当我听它的时候,我发现我一直在得到所有的东西。我只需要最新的文档,我需要在添加新文档时响应。我试图像这样限制查询:
this.itemsCollection = this.afs.collection('games').doc(gameId)
.collection('hands', ref => ref.orderBy('started').limit(1));
this.items = this.itemsCollection.valueChanges();
this.items.subscribe((hands: Hand[]) => {
console.log('got hand ', hands);
});
遗憾的是,它不起作用。添加新项目时我没有收到任何项目。当我删除限制功能时,我每次都得到所有项目...
最简单的实现方法是为集合中的每个对象添加一个 Date 属性,然后根据这个新的 属性 降序简单地查询它并调用 limit(1) 函数。您可以查看有关如何 "Order and limit data with Cloud Firestore" and "Get data with Cloud Firestore"
的文档思路如下:
db.collection("YourCollection")
.orderBy('Date', 'desc')
.limit(1).get()
})
如果有帮助请告诉我。