skip to Main Content

I have a query:

const q = query(
  collection(db, '/listings'),
    where('price', '>=', 4000),
  orderBy('price', 'desc'),
  orderBy('geoHash'),
  startAt(b[0]),
  endAt(b[1]),
  limit(DEFAULT_LIMIT_OF_LISTINGS),
) as Query<IListing>;

If I remove

where('price', '>=', 4000),"

it works fine with the geoHash condition.

if I remove geoHash condition it works fine as well with the price condition.

Why they are not working together?

I expect to get all documents with a price greater than 4000 in the given area.

2

Answers


  1. Few restrictions are applied to .orderBy() parameter, have a look at the official documentation.

    Here in this case, the you can only order by price and not geohash, if I understood the concept correctly, please go through official docs.

    Login or Signup to reply.
  2. Firestore queries can only contain one relational condition (>=, >, etc) because such conditions can only be evaluated on the first field in an index. Since you need a relational/range condition for the geohash already, you can’t also have a >= condition on price.

    The common options to work around this are:

    • Perform the filter on the second condition in your application code, so that you first get all documents that are in range, and then in your application remove the ones whose price is out of range.
    • Add a field to your database that allows the use-case you want. For example, if you add a field isPriceOver4000: true you can use an equality condition .where('isPriceOver4000', '==', true).

    That last option may feel wrong, but is actually quite common when using NoSQL to modify and augment your data model to fit with your use-case. Of course you’ll want to find the best model for your needs, for example you might want an array (or map subfield) of price tags that users can filter on.

    Alternatively, you can create similar buckets of regions, and query the location on that instead of geohash, and then use the >= on price.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search