简体   繁体   English

通过用户ID和日期范围查询Firestore?

[英]Query Firestore by UserID and date range?

I need to query Firestore, but it keeps telling me I need an index. 我需要查询Firestore,但它总是告诉我我需要索引。 Problem is that isn't realistic, since EVERY user would require a custom index based on their UserId. 问题在于这是不现实的,因为每个用户都需要基于其UserId的自定义索引。 For security, I can't allow anyone access unless canAccess.${userID} == true as in the example below. 为了安全起见,除非下例中的canAccess.${userID} == true ,否则我不允许任何人访问。 But if I were to take ALL items, and filter them by date client-side I would be fetching hundreds of thousands of items every time I change the date range, so it isn't practical. 但是,如果我要处理所有项目,并按日期在客户端进行过滤,那么每次更改日期范围时,我都会获取数十万个项目,因此这是不切实际的。

this.db.collection('items')
       .where(`canAccess.${userId}`, '==', true)
       .where('date', '>=', startDate)
       .where('date', '<=', endDate);

How would one going about fetching only the items they are allowed to access, and only between the chosen date range? 如何仅在选定的日期范围内仅获取允许访问的项目?

You'll have to restructure your documents in order to perform this query. 您必须重组文档才能执行此查询。 Consider putting the uids that can access the document in a field with a List type called canAccess . 考虑将可以访问文档的uid放在名为canAccess的List类型的canAccess Then query the list with array-contains after you create an index on that field. 然后,在该字段上创建索引后,查询array-contains的列表。

this.db.collection('items')
       .where('canAccess', 'array-contains', userId)
       .where('date', '>=', startDate)
       .where('date', '<=', endDate);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM