如何在 Firestore 中使用 android 的多个文档字段进行搜索?

Sam*_*yer 5 android firebase google-cloud-firestore

我正在使用适用于 android 的 Firebase Firestore 来存储数据。我试图从文档中搜索数据。

我的 Firestore 结构是:

集合(产品)- 文档(自动生成的 ID)- 字段(NumberOfPeople,OfferStartDate,OfferEndDate,OfferPrice,Location)

我为这些字段的搜索数据编写了查询。

CollectionReference collectionOfProducts = db.collection("Products);

collectionOfProducts
                    .whereEqualTo("Location", location)
                    .whereGreaterThanOrEqualTo("OfferPrice", offerPrice)
                    .whereLessThanOrEqualTo("OfferPrice", offerPrice)
                    .whereGreaterThanOrEqualTo("OfferStartDate", date)
                    .whereLessThanOrEqualTo("OfferEndDate", date)
                    .get()
Run Code Online (Sandbox Code Playgroud)

我想要这样的搜索结果:在开始日期和结束日期之间的报价,其中报价在给定价格范围内大于等于或小于等于。此查询在 android studio 中不起作用。

如何在firestore firebase中做到这一点?

Kar*_*odi 1

据我所知,Firestore 只允许您使用 where<Greater/Less>ThanOrEqualTo() 和 where<Greater/Less>Than() 单个字段,而其他字段上的所有其他筛选操作只能是 whereEqualTo()。

针对您的具体情况的一些解决方法包括 -

1)将您的查询修改为

collectionOfProducts
                    .whereGreaterThanOrEqualTo("OfferStartDate", date)
                    .whereEqualTo("Location", location)
                    .get() 
Run Code Online (Sandbox Code Playgroud)

然后在应用程序代码中对结果执行后续过滤。

或者,您可以对查询中的“OfferPrice”和“Location”执行过滤器,其余过滤器可应用于查询结果。

2) 您可以使用firebase 函数或其他服务器代码来编写执行自定义过滤的逻辑并以这种方式获取结果。