1

I have this code where I am trying to fetch a document from a collection where time is not older than half an hour ago. It fails exactly in the inequality operator.

try {
  const HALF_HOUR = 0.1 * 6000 * 6000 / 2;
  const tsToMillis = firebase.firestore.Timestamp.now().toMillis();
  const currenttime = new Date(tsToMillis - HALF_HOUR);

  const myCurrentLoc = location[0]; // my device location
  const latitude = myCurrentLoc.latitude;
  const longitude = myCurrentLoc.longitude;

  const geocollection = GeoFirestore.collection('chats');
  const query = await geocollection.limit(1).near({ center: new firebase.firestore.GeoPoint(latitude, longitude), radius: 0.03 });
  query.where('details.time', '>=', currenttime).get().then((querySnapshot) => {
    if (!querySnapshot.empty) {
      querySnapshot.docs.forEach(documentSnapshot => {
        if (documentSnapshot.exists) {
          this.fetchAllMessages(documentSnapshot.id);
        }
      });
    }
  }).catch(error => {
    console.error("Error in fetch_messages", error);
  });
} catch (e) {
  console.error("Something happened: ", e); // ERROR IS TRIGGERED HERE
}

C:\MyProjects\BLEConnect\node_modules\react-native\Libraries\Core\ExceptionsManager.js:180 Something happened: Error: firebase.firestore().collection().orderBy() Invalid query. Initial Query.orderBy() parameter: g.geohash has to be the same as the Query.where() fieldPath parameter(s): details.time when an inequality operator is invoked

This error occurred only when invoking inequality operator but not "==" equal operator. Why so? I simply dont get it.

P.S.: the "time" field is added when the geohash has been created, so in the same query:

const tsToMillis = firebase.firestore.Timestamp.now().toMillis();
const currenttime = new Date(tsToMillis);
const geocollection = GeoFirestore.collection('chats');
geocollection.add({
  name: 'Geofirestore',
  uid: device_id,    
  coordinates: new firebase.firestore.GeoPoint(latitude, longitude),
  time: currenttime
})
2
  • 1
    Please copy-and-paste the actual error. I'm having a hard time believing an error would say "Somethign happened". (And if that's the literal error, the firebase folks have some explaining to do) Dec 5, 2021 at 0:41
  • @Mike'Pomax'Kamermans I did but I guess I missed that part somehow.
    – showtime
    Dec 5, 2021 at 1:05

2 Answers 2

2

From the Firestore documentation on query limitations:

In a compound query, range (<, <=, >, >=) and not equals (!=, not-in) comparisons must all filter on the same field.

The way the inequality operators are implemented requires that you order on that field first. For the equality operator Firestore is able to do more on its own, for example combining information from multiple indexes in a so-called zig-zag-merge-join.

7
  • sorry Frank, not sure if I got you. Can you please explain again using my code?
    – showtime
    Dec 5, 2021 at 1:08
  • you mean have an .OrderBy() ...
    – showtime
    Dec 5, 2021 at 1:09
  • @showtime: Read very carefully the title of your question (I have edited in the relevant part of the error message). Dec 5, 2021 at 1:10
  • 1
    There's not really much more to get than what the error message says: in order to use an inequality operation on your query, you have to have a orderBy on that field as the first orderBy call for the query. This also means that you can't combine such a condition (or range) with a geo-query, as that already requires >= and <= operators, and as the quote from the documentation says: all range and inequality operators must be on the same field. If you'd like to learn why, here's a (quite long) talk where I explain how geoqueries are implemented: youtube.com/watch?v=mx1mMdHBi5Q Dec 5, 2021 at 1:10
  • 1
    That is correct: if you watch the video I linked, you'll find why it's already quite magic that you can query by location, which normally would require a range (>= etc) condition on both lat and lon, which Firestore can't do. Adding yet an another, unrelated, value to the range query is unfeasible. Dec 5, 2021 at 2:44
0

You can do something like this, pay attention to the ´native´ which extends the native queries of firestore:

const geocollection = GeoFirestore.collection('myCollection');
const query = geocollection.near({ center: new firebase.firestore.GeoPoint(latitude, longitude), radius: 0.05 })
              .native.where("created", ">", thirty_mins_old).orderBy("created", "desc");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.