调用不等运算符时,初始 Query.orderBy() 参数必须与 Query.where() fieldPath 参数相同

Initial Query.orderBy() Parameter has to be the same as the Query.where() fieldPath parameter(s) when an inequality operator is invoked

我有这段代码,我试图从时间不早于半小时前的集合中获取文档。它在不等式运算符中完全失败。

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

此错误仅在调用不等运算符而不是“==”等运算符时发生。为什么这样?我就是不明白。

P.S.: 在创建geohash时添加“时间”字段,所以在同一个查询中:

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
})

来自 query limitations 上的 Firestore 文档:

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

inequality 运算符的实现方式要求您先在该字段上排序。对于相等运算符,Firestore 能够自行完成更多工作,例如,在所谓的之字形合并连接中组合来自多个索引的信息。

你可以这样做,注意扩展 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");