【问题标题】:Initial Query.orderBy() Parameter has to be the same as the Query.where() fieldPath parameter(s) when an inequality operator is invoked调用不等式运算符时,初始 Query.orderBy() 参数必须与 Query.where() fieldPath 参数相同
【发布时间】:2021-12-05 00:26:58
【问题描述】:

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

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 发生了一些事情:错误:firebase.firestore().collection().orderBy() 查询无效。初始 Query.orderBy() 参数:g.geohash 必须与 Query.where() fieldPath 参数相同:在调用不等式运算符时的 details.time

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

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

【问题讨论】:

  • 请复制并粘贴实际错误。我很难相信错误会说“发生了一些事情”。 (如果这是字面上的错误,firebase 人员需要做一些解释)
  • @Mike'Pomax'Kamermans 我做了,但我想我错过了那部分。

标签: javascript firebase react-native google-cloud-firestore geofirestore


【解决方案1】:

来自 query limitations 上的 Firestore 文档:

在复合查询中,范围(<<=>>=)和不等于(!=not-in)比较都必须在同一字段上进行过滤。

inequality 运算符的实现方式要求您首先在该字段上排序。对于相等运算符,Firestore 可以自己做更多事情,例如在所谓的 zig-zag-merge-join 中组合来自多个索引的信息。

【讨论】:

  • 对不起,弗兰克,我不确定我是否得到你。你能用我的代码再次解释一下吗?
  • 你的意思是有一个 .OrderBy() ...
  • @showtime:仔细阅读你的问题标题(我已经在错误信息的相关部分进行了编辑)。
  • 没有什么比错误消息所说的更多了:为了在查询中使用不等式运算,您必须在该字段上使用 orderBy 作为第一个 orderBy来电查询。这也意味着您不能将这样的条件(或范围)与地理查询结合起来,因为这已经需要 >=<= 运算符,并且正如文档中的引用所说:所有范围和不等式运算符都必须在同一个领域。如果你想了解原因,这里有一个(相当长的)演讲,我解释了地理查询是如何实现的:youtube.com/watch?v=mx1mMdHBi5Q
  • 那是正确的:如果您观看我链接的视频,您会发现为什么可以按位置查询已经很神奇了,这通常需要一个范围(>= 等)条件Firestore 无法做到的纬度和经度。向范围查询添加另一个不相关的值是不可行的。
【解决方案2】:

你可以这样做,注意扩展firestore原生查询的“native”:

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");

【讨论】:

    猜你喜欢
    • 2021-09-10
    • 1970-01-01
    • 2011-06-25
    • 2018-04-24
    • 2015-06-04
    • 2020-10-15
    • 1970-01-01
    • 2018-04-25
    • 2011-02-25
    相关资源
    最近更新 更多