问题描述
在我的网站上,我正在进行像tests
这样的调查,每个测试的attendies
子收藏集都像这样
In my site i am conducting a survey like tests
, each test has attendies
sub collection look like this
当某人完成测试时,我也将他们的uid
添加到completed
字段中,就像我在框中绘制的一样.现在我想基于status == completed
查询tests
.
When someone finishes a test i also add their uid
to completed
field like i drawn in the box. Now i want to query tests
based on status == completed
.
这是我尝试过的
this.completedModulesRef$ = this.afs.collection('tests', ref =>
ref.orderBy('moduleNum', 'desc')
.where('completed.'+auth.uid+'.status','==','completed'));
this.completedModules$ = this.completedModulesRef$.valueChanges();
然后,firestore要求我添加索引,当我按照生成的链接添加索引时,我知道了
Then firestore asked me to add indexes, when i follow the generated link to add indexes i got this
指向completed.CurrentUserId.status
.我相信这仅适用于当前用户.
which is pointing to completed.CurrentUserId.status
. I believe this only work for current user.
我有几个问题
1).where('completed.'+auth.uid+'.status','==','completed')
这是有效查询吗?
1) .where('completed.'+auth.uid+'.status','==','completed')
Is this a valid query?
2)如果是,我如何索引它?
2) If yes how can i index it?
3)是否可以根据子集合值查询顶部集合? (这就是我真正想要的)
3) Any way to query the top collections based on sub collection value?. (this is what i really want)
任何帮助表示赞赏.
推荐答案
您可以改为使用一组对象(每个对象代表一个与会者)来跟踪所有已完成的与会者.数组在Firestore中建立索引.
You could just instead use an array of objects (each object represents one attendie) to track all the completed attendies. Arrays are indexed in firestore.
该数组的数据结构为:
compeleted: [{}] = [
{id: string, points: number, status: string}
]
根据要获取数据的方式和位置,这可能不是最佳数据库模型,但是它将被索引并且可以查询它.我会考虑将积分和状态存储在与会者的子集合中.看看firestore中的分组集合查询-一种新功能,您可以一次在所有出席者子集合中查询-如果要获取一个参加者完成的所有测试,则具有特定ID且状态为完成"的任何参加者.
This might not be the most optimal database model depending on how and where you want to fetch the data but it will be indexed and you will be able to query it. I would consider storing the points and status in the subcollection of attendies. Have a look at grouped collection queries in firestore - new feature where you can query across all attendies subcollections at once - for any attendie with a certain id and a status of complete if you want to fetch all the tests that one attendie completed.
这篇关于如何使用Firestore执行动态Where查询并添加索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!