获取集合中的最新文档

时间:2019-11-22 06:58:51

标签: google-cloud-firestore angularfire2

我有一件物品。当我听的时候,我看到我一直在拿所有的东西。我只需要最新的文档,添加新文档时需要回复。我试图这样限制查询:

    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);
    });

不幸的是,它不起作用。添加新项目时,我没有任何项目。取消限制功能后,每次都会得到所有物品...

1 个答案:

答案 0 :(得分:1)

实现此目的的最简单方法是将Date属性添加到集合中的每个对象,然后根据此新属性降序并调用limit(1)函数简单地对其进行查询。您可以查看有关如何"Order and limit data with Cloud Firestore""Get data with Cloud Firestore"

的文档

想法如下:

>>

让我知道这是否有帮助。

相关问题