[英]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()
函数的注册按钮时,不存在的祖先用户似乎是自动创建的( fred
和lisa
是手动创建的)。
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/subDoc1
的col1
集合下创建文档,则不会创建任何中间文档(即没有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-)collection和
subCol1
(子)集合下的另一个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.