简体   繁体   English

如何获取 Firebase 集合中的所有文档(包括不存在的祖先文档)?

[英]How do you fetch all documents (including non-existent ancestor documents) in a Firebase collection?

I am trying to pull all the documents in the collection 'users', but it only pulls 'fred' and 'lisa', and ignores all the italicized documents:我试图提取“用户”集合中的所有文档,但它只提取“fred”和“lisa”,并忽略所有斜体文档:

For this data:对于此数据:

在此处输入图像描述

Trying to get all documents:试图获取所有文件:

在此处输入图像描述

Will yield:将产生:

info: length 2
info: fred  =>  { gender: 'male', contacts: [ '' ] }
      lisa  =>  { contacts: [ '' ] }

According to the Firebase documentation ( Firebase: Add and Manage Data ):根据 Firebase 文档( Firebase:添加和管理数据):

Warning: Even though non-existent ancestor documents appear in the console, they do not appear in queries and snapshots.警告:即使不存在的祖先文档出现在控制台中,它们也不会出现在查询和快照中。 You must create the document to include it in query results.您必须创建文档以将其包含在查询结果中。

Note: The non-existent ancestor users seem to be auto-created when the user hits the sign-up button that triggers a firebase.auth() function ( fred and lisa were created manually).注意:当用户点击触发firebase.auth()函数的注册按钮时,不存在的祖先用户似乎是自动创建的( fredlisa是手动创建的)。

How would I print the contacts of each user if some of the users do not show up in my queries?如果某些用户没有出现在我的查询中,我将如何打印每个用户的contacts Would I need to periodically run a script that manually re-adds all the users or is there a more elegant solution?我是否需要定期运行手动重新添加所有用户的脚本,或者是否有更优雅的解决方案?

As you have mentioned, these "documents" are displayed with an italic font in the Firebase console: this is because these documents are only present (in the console) as "container" of one or more sub-collection but they are not "genuine" documents.正如您所提到的,这些“文档”在 Firebase 控制台中以斜体显示:这是因为这些文档仅作为一个或多个子集合的“容器”存在(在控制台中),但它们不是“真正的” ”文件。

As matter of fact, if you create a document directly under a col1 collection with the full path doc1/subCol1/subDoc1 , no intermediate documents will be created (ie no doc1 document).事实上,如果您直接在具有完整路径doc1/subCol1/subDoc1col1集合下创建文档,则不会创建任何中间文档(即没有doc1文档)。

The Firebase console shows this kind of "container" (or "placeholder") in italics in order to "materialize" the hierarchy and allow you to navigate to the subDoc1 document but doc1 document doesn't exist in the Firestore database. Firebase 控制台以斜体显示这种“容器”(或“占位符”),以便“具体化”层次结构并允许您导航到subDoc1文档,但doc1文档在 Firestore 数据库中不存在。

Let's take an example: Imagine a doc1 document under the col1 collection举个例子:假设col1集合下有一个doc1文档

col1/doc1/

and another one subDoc1 under the subCol1 (sub-)collectionsubCol1 (子)集合下的另一个subDoc1

col1/doc1/subCol1/subDoc1

Actually, from a technical perspective, they are not at all relating to each other.实际上,从技术的角度来看,它们完全没有关系。 They just share a part of their path but nothing else.他们只是共享他们路径的一部分,除此之外别无其他。 One side effect of this is that if you delete a document, its sub-collection(s) still exist .这样做的一个副作用是,如果您删除文档,它的子集合仍然存在

So, if you want to be able to query for these parent documents, you will have to create them yourself, as jackz314 mentioned in the comments.因此,如果您希望能够查询这些父文档,则必须自己创建它们,如评论中提到的 jackz314。

If you're trying to list all your registered users from Firebase auth, you can use the Firebase SDK function:如果您尝试从 Firebase 身份验证中列出所有注册用户,您可以使用 Firebase SDK 函数:

    function listAllUsers(nextPageToken) {
      admin.auth().listUsers(1000, nextPageToken)
        .then(function(listUsersResult){
          listUsersResult.users.forEach(function(userRecord) {
            console.log('user', userRecord.toJSON());
          })
          if (listUsersResult.pageToken) {
            // list next batch of users
          }
        })
        .catch(function(err) {
          console.log('Error listing users: ', error)
        });
    }

    listAllUsers();

via http://firebase.google.com/docs/auth/admin/manage-users#list_all_users通过http://firebase.google.com/docs/auth/admin/manage-users#list_all_users

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 firestore 集合中获取文档列表 - How do you fetch a list of documents from a firestore collection 如何从 firebase 中的集合中的所有文档中获取特定数据? - How to get specific data from all documents from a collection in firebase? 如何检索 Firebase 中集合中的所有文档并添加到列表中? - How to retrieve all documents in a collection in Firebase and add to a list? Firebase 函数:更新集合中的所有文档 - Firebase Functions: update all documents inside a collection 如何一次将多个文档添加到 Firestore 中的一个集合中? - How do you add multiple documents at once to a collection in Firestore? 您如何使用 admin SDK 计算 firestore 集合中的文档? - How do you count documents in a firestore collection using the admin SDK? 如何获取 Firebase 集合中具有特定值的所有文档? - How to get all documents in Firebase collection with specific value? 如何通过属性值查询 firebase 个文档的集合? - How do I query a collection of firebase documents by a properties value? firebase云店如何复制收款文件? - How do I copy collection documents in firebase cloud store? 将所有项目作为单独的文档保存到 firebase 集合 - Saving all items to a firebase collection as individual documents
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM