Home > Software engineering >  firebase range query with more than 2 fields which is array
firebase range query with more than 2 fields which is array

Time:06-09

i'm making a feature like search on map by latitude and longitute.

my firebase field for it looks like this: enter image description here

eventLoc field is array field.

and for example, i have current map bounds data like this :

bounds : {
  max_lat: 37.56416769262514,
  min_lat: 37.55697370580578,
  max_lng: 127.04739120688566,
  min_lng: 127.03271908496473
}

so i've tried making query like this:

enter image description here

(i wanted to get documents if the array eventLoc fields contain the lat/lng that was conditional, is this query right? not 'evenLoc.lat[0]' ? )

but i got an error:

FirebaseError: Invalid query. All where filters with an inequality (<, <=, !=, not-in, >, or >=) must be on the same field. But you have inequality filters on 'eventLoc.lat' and 'eventLoc.lng'

so what can i do if i can't use more than 2 fields for search, is there no way to get ranged data by longitude and latitude with my database structure?

CodePudding user response:

There is nothing built into Firestore for this type of query, but you can use the approach outline in the documentation on geoqueries to build it yourself on top of Firestore.

This solution works by adding additional data to each document encoding the each lat/lon pair into a so-called geohash, and then performing a range query on the geohash.

A few things to keep in mind:

  • You'll typically need to perform multiple queries for a specific range, as the geohash area likely don't exactly match your bounds.
  • You'll read more documents than needed, since the geohash is likely bigger than your bounds, which is why the link shows how to further cull the results in the application code.
  • Related