Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

javascript - firestore.where() on two fields at once

I have a firestore collection of the following documents:

[
  {
    start:       { geohash: 'u3qchtmpuy2d' },
    destination: { geohash: 'u3qcjvxfh9cs' },
    timestamp:   '28 June 2019 at 20:24:00 UTC+2',
    ...
  }
]

and I tried to query it like this (its Firestore Web SDK)

// 5-characters geohash is 4.89km × 4.89km
const start = 'u3qch';
const destination = 'u3qcj';
const timestamps = {
  min: firestore.Timestamp.fromDate(
    moment(someDateTime).subtract(10, 'minutes').toDate()
  ),
  max: firestore.Timestamp.fromDate(
    moment(someDateTime).add(10, 'minutes').toDate(),
  ),
};

firestore
  .collection('deliveries')
  .where('start.geohash', '>=', start)
  .where('start.geohash', '<', geohashEnd(start))
  .where('destination.geohash', '>=', destination)
  .where('destination.geohash', '<', geohashEnd(destination))
  .where('timestamp', '>=', timestamps.min)
  .where('timestamp', '<', timestamps.max)
  .get()

the combination of >= and < is from Firestore: query documents by startsWith a string, hence geoHashEnd() function (out of the scope of this question).

It resulted in the following error:

FirebaseError: Invalid query. All where filters with an inequality (<, <=, >, or >=) must be on the same field. But you have inequality filters on 'start.geohash' and 'destination.geohash'

My question: what is the best approach to query my firestore collection by two geohash strings and an additional field, at once?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...