firebase - 在 Firestore [通配符] 中为点注释的动态路径创建索引?
问题描述
我的收藏创建如下:
-products
-productID
-category [object]
catitem1 - boolean
catitem2 - boolean
现在我写了一个查询如下
this.afs.collection<Product>('products', ref =>
ref
.where(`category.${categoryName}`, '==', true)
.limit(limit)
);
此查询工作正常,但是当我将 orderBy 添加到上述查询时,我被要求index
为查询创建一个。
this.afs.collection<Product>('products', ref =>
ref
.where(`category.${categoryName}`, '==', true)
.orderBy('createdDate','desc')
.limit(limit)
);
由于categoryName
可以创建并且可以随时更改,我应该为每个 categoryName 添加索引,这将是数百个。
有什么方法可以创建通配符索引category.categoryName
吗?
我尝试使用category.*
,但这是不可接受的。希望能在这里找到一些帮助。
解决方案
自从提出这个问题以来,Firestore 已经实现了各种运算符来帮助查询数组成员资格。在撰写本文时可用的运算符有:array-contains
、array-contains-any
、in
和not-in
。
您可以拥有一个仅包含产品所属类别的名称(或者,如果您想节省空间,则为id)的数组,而不是将其存储category
为eachobject
的属性。Boolean
categoryName
categories
String
Int
然后查询可以像这样工作,只需要一个索引:
this.afs.collection<Product>('products', ref =>
ref
.where('categories', 'array-contains', categoryName)
.orderBy('createdDate','desc')
.limit(limit)
);
推荐阅读
- python - Django 下载文件按钮采用路径
- django - 'PdfFileWriter' 对象没有属性 'seek'
- typeclass - 如何在 Isabelle 的类型类定义中有多个任意类型?
- heroku - 如何检查已部署的烧瓶 Web 应用程序的数据库
- c# - ASP.NET MVC:SQL Db 作为 IEnumerable 对象的基于方法的 LINQ 查询
- javascript - 使用 Jest 在 Next.JS 中测试 ClientPortal
- html - 需要帮助添加飞机上线
- python - Python 中的数据整理以创建列表
- windows - rm:无法删除“a.txt”:参数无效
- javascript - 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于在 Object.keys 中进行索引