Firestore | Why do all where filters have to be on the same field?

左心房为你撑大大i 提交于 2020-12-12 04:58:23

问题


Apparently Firestore does not not support queries with range filters on different fields, as described here:

My question is, WHY?

HOW can I query by time and location for example:

Firestore.instance.collection('events')
      .where("eventTime", isGreaterThanOrEqualTo: DateTime.now().millisecondsSinceEpoch)
      .where("eventLocation", isGreaterThan: lesserGeopoint)
      .where("eventLocation", isLessThan: greaterGeopoint)
      .where("eventStatus", isEqualTo: "created")
      .orderBy('eventLocation', descending: false)
      .orderBy('eventTime')
      .snapshots(),

(Example from Flutter App written in Dart)

I receive the following Error:

All where filters other than whereEqualTo() must be on the same field. But you have filters on 'eventTime' and 'eventLocation', null)

I don't understand how this is not supported and how to solve queries like this?

Any help is appreciated :-)

Thanks, Michael


回答1:


Cloud Firestore gives a strong performance guarantee for any read operations it allows: the time a read operation takes depends on the number of items you're reading, not on the number of items in the collection. This is a quite unique performance guarantee, since it means that your queries will take the same amount of time when you have 1 million users as when you have 1 billion users.

Firestore only offers query operations for which it can maintain this performance guarantee. This is the main reason for the limitations you may find in the Firestore query API.

To work around the limit, you'll typically perform the filtering, and ordering on the primary field in the query, and then sort on the secondary field client-side

If you'd like to learn more about Firestore's query capabilities and limitations, I highly recommend watching How do queries work in Cloud Firestore? and the rest of the Get to Know Cloud Firestore video series.

Also see:

  • Firestore order by two fields
  • Firestore order by two fields in one query



回答2:


Have you had a read through the page on indexes, which describes how some compound queries can be supported, and how composite indexes can be created and used?



来源:https://stackoverflow.com/questions/54969057/firestore-why-do-all-where-filters-have-to-be-on-the-same-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!