且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从 Firestore 上的文档中获取所有集合 ID?

更新时间:2023-02-14 08:08:10

不幸的是,您想要执行的操作目前在移动/网络客户端中无法实现.

Cloud Firestore 服务器客户端的 getCollections() 方法图书馆列出了文档引用的所有子集.

无法通过移动/网络检索集合列表客户端库.您应该只查找集合名称作为可信服务器环境中的管理任务.如果你发现您需要移动/网络客户端库中的此功能,请考虑重组您的数据,以便子集合名称是可预测的.

来源:列出文档子集>

I connect the database from React Native project.

In my Firestore database Buildings/Taipei, i want to get all of the collection ids like 201 202 203

I try to use the code:

const db = firebaseApp.firestore();

    db.collection('Buildings').doc('Taipei')
    .get()
    .then(doc => {
      console.log('get doc is =>');
      console.log(doc);
    }).catch(error => {
      console.log(`error is ${error}`);
    });

I can't see any collection id in the doc.

Any help would be appreciated. Thanks in advance.

Unfortunately what you want to do is not currently possible in the mobile/web client.

The getCollections() method of the Cloud Firestore server client libraries lists all subcollections of a document reference.

Retrieving a list of collections is not possible with the mobile/web client libraries. You should only look up collection names as part of administrative tasks in trusted server environments. If you find that you need this capability in the mobile/web client libraries, consider restructuring your data so that subcollection names are predictable.

Source: List Subcollections of Document