get()方法对CollectionReference有什么作用?

时间:2019-11-01 18:00:12

标签: javascript node.js google-cloud-firestore

当我们查看用于Firesore CollectionReference对象的NodeJS API时,我们发现它具有一个称为get()的方法。在此处记录:

https://googleapis.dev/nodejs/firestore/latest/CollectionReference.html#get

并指出:

  

执行查询并将结果作为QuerySnapshot返回。

页面上的示例代码显示为:

let query = firestore.collection('col').where('foo', '==', 'bar');

query.get().then(querySnapshot => {
  querySnapshot.forEach(documentSnapshot => {
    console.log(`Found document at ${documentSnapshot.ref.path}`);
  });
});

但是,这让我感到困惑。在示例代码中,get()方法是在类型Query的对象上执行的,而不是在CollectionReference类型的对象上执行的,因此似乎不适用。我缺少对get()上的CollectionReference方法执行的 query 查询。

1 个答案:

答案 0 :(得分:1)

API documentation for CollectionReference(向下滚动)中可以看到,CollectionReference 是一个查询,因为它继承自查询。还有第二个称为“扩展”,您将在其中看到查询。该查询将返回集合中的所有文档。当您对该CollectionReference查询调用where()时,它将返回一个应用了过滤器的新查询。