javascript - 使用 Cloud Firestore 的 Ionic App - 查看所有用户的数据
问题描述
我的数据结构如下:
collection(people)
document (UserId1)
collection(profiles)
document (generatedId1)
name: 'Speech1'
type: 'admin'
document (UserId2)
collection(profiles)
document (generatedId2)
name: 'Client1'
type: 'client'
collection(tasks)
document (generatedId3)
date: '2018-12-30'
time: 'AM'
severity: 0
document (generatedId4)
date: '2018-12-30'
time: 'PM'
severity: 0
document (UserId3)
collection(profiles)
document (generatedId5)
name: 'Client2'
type: 'client'
collection(tasks)
document (generatedId5)
date: '2018-12-30'
time: 'AM'
severity: 0
document (generatedId6)
date: '2018-12-30'
time: 'PM'
severity: 0
这种结构允许我使用如下语句查询登录用户的任务集:
getTasks(){
return new Promise<any>((resolve, reject) => {
let currentUser = firebase.auth().currentUser;
this.snapshotChangesSubscription = this.afs.collection('people').doc(currentUser.uid).collection('tasks').snapshotChanges()
.subscribe(snapshots => {
resolve(snapshots);
})
});
}
但是,当我以管理员类型用户身份登录时,我希望查看所有用户的数据,因此我尝试构建和查询本质上将考虑人员集合中的所有文档的查询。
我怎样才能做到这一点?
解决方案
但是,当我以管理员类型用户身份登录时,我希望查看所有用户的数据,因此我尝试构建和查询本质上将考虑人员集合中的所有文档的查询。
我怎样才能做到这一点?
不幸的是,您不能使用此数据库模式。Firestore 中的查询很浅,这意味着它们只能从运行查询的集合中获取项目。无法在单个查询中从顶级集合和其他集合或子集合中获取文档。Firestore 不支持一次性跨不同集合进行查询。单个查询只能使用单个集合中文档的属性。所以我能想到的最简单的解决方案是添加另一个集合,您应该在其中添加所有存在于 all 中的所有子集合中的所有“文档” people collection -> uid -> generatedId
。因此,您的架构可能与此类似:
Firestore-root
|
--- allDocuments (collection)
|
--- generatedId1 (document) //The unique id of the document
| |
| --- name: 'Speech1'
| |
| --- type: 'admin'
| |
| --- uid: 'UserId1'
|
--- generatedId 2(document) //The unique id of the document
|
--- name: 'Client1'
|
--- type: 'client'
|
--- uid: 'UserId2'
为了显示所有这些用户,只需在allDocuments
引用上附加一个侦听器并获取所有users
对象。如果您想获取所有管理员用户,只需使用Query
如下所示的:
var allDocumentsRef = db.collection("allDocuments");
var query = allDocumentsRef.where("type", "==", "admin");
推荐阅读
- c# - I am trying to check that If email already exist in databse or not or not in database but my code throws error
- flutter - 当我在 FutureBuilder 中调用 ListView.builder 时,总是崩溃
- google-sheets - 谷歌表格 - 计算有多少行符合这些条件
- laravel - 在laravel中用新图像替换旧图像
- javascript - 如何在条形图中每个堆栈的顶部和底部写入标签以及数据
- javascript - 获取仅包含定义的“禁用”属性的列表
- ubuntu - Nginx 网站在本地主机上工作,但不在 IP / 域名上
- java - DateFormatSymbols 给出了错误的月/日翻译
- testing - 赛普拉斯因“Http 失败响应”而随机失败
- react-native-android - react-native android构建缓慢