如何获取集合Cloud Firestore中的文档ID列表? [英] How to get a list of document IDs in a collection Cloud Firestore?

查看:87
本文介绍了如何获取集合Cloud Firestore中的文档ID列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有生成的标识符的文档集合.问题是:是否可以在不查询所有文档数据的情况下获得标识符列表?最好将这些密钥存储在单独的集合中吗?

I have a collection of documents with generated identifiers. The question is: is it possible to get a list of identifiers without querying all documents data? Is it better to store these keys in a separate collection?

推荐答案

答案取决于您要使用的API.

The answer depends on which API you're trying to use.

对于移动/网络SDK ,由于这些客户端

For mobile/web SDKs there is no way to do what you're asking for since these clients do not support projections of any kind.

对于服务器SDK ,您可以进行空投影,即

For server SDKs you can do an empty projection, i.e.

db.collection('foo').select()

在这种情况下,服务器将向您发送匹配的文档,但会省略查询结果中的所有字段.

In this case the server will send you the documents that match, but will omit all fields from the query result.

对于 REST API ,您可以使用runQuery进行等效操作,其中包括'__name__'的字段掩码,如下所示:

For the REST API you can do the equivalent with a runQuery that includes a field mask of '__name__', like so:

curl -vsH 'Content-Type: application/json' \
  --data '{
    "parent": "projects/my-project/databases/(default)",
    "structuredQuery":{
      "from": [{"collectionId": "my-collection"}],
      "select": {
        "fields": [{"fieldPath":"__name__"}]
      }
    }
  }' \
  'https://firestore.googleapis.com/v1beta1/projects/my-project/databases/(default)/documents:runQuery'

适当地替换my-projectmy-collection.请注意,"from"中的"collectionId"只是最右边的名称组件.如果要在子集合中使用键,则REST API希望在"parent"字段中使用父文档名称.

Substitute my-project and my-collection as appropriate. Note that the "collectionId" in the "from" is only the right most name component. If you want keys in a subcollection the REST API wants the parent document name in the "parent" field.

这篇关于如何获取集合Cloud Firestore中的文档ID列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆