在嵌套结构中使用 AngularFire 查询 Relatime 数据库中的数据

问题描述 投票:0回答:1

我在 Firebase 实时数据库中有以下路径

/数据/userId/dayDate/tableID/rowWithData

rowWithData的结构如下

{
    "-NLBD8PD7i5DPfdgF": {
      "name": "Demo 1",
      "rows": {
        "-NLBD8RiJeyZpnKg8iKI": {
          "lastEdit": 1676495720665,
          "name": "Data 1",
          "priority": 1
        },
        "-NLBPODZMfFFXKTFDo7p": {
          "lastEdit": 1676495720665,
          "name": "Data 2",
          "priority": 5
        }
      }
  },
  "-NLBD8PD7asdas": {
    "name": "Demo 2",
    "rows": {
      "-NLBD8RiJeyZpnKg8iKI": {
        "lastEdit": 1676495720665,
        "name": "Data 3 here",
        "priority": 1
      },
      "-NLBPODZMfFFXKTFDo7p": {
        "lastEdit": 1676495720665,
        "name": "Data 4 here",
        "priority": 5
      }
    }
  }
}

我正在尝试以下查询。 第一个返回 0

this.angularFireDatabase
  .list(`dataTables/${this.userInfo.uid}`, ref => ref.limitToFirst(50).equalTo(`search term`))
  .snapshotChanges()
  .subscribe(data => {
    console.log("search data", data);
  })   

第二个返回/data/userId/dayDate这个路径下数据库的所有数据

this.angularFireDatabase
  .list(`dataTables/${this.userInfo.uid}`, ref => ref.orderByChild('rows'))
  .snapshotChanges()
  .subscribe(data => {
    console.log("search data", data);
    debugger;
  }) 

当我将 .equalsTo() 函数添加到任何术语时,它不会返回任何结果。

我正在尝试在路径 /data/userId 中搜索搜索词中的所有值。

这里有一些信息,但我没有找到任何适合我的情况的实用信息。

https://github.com/angular/angularfire/blob/master/docs/compat/rtdb/querying-lists.md https://firebase.google.com/docs/database/web/lists-of-data#sorting_and_filtering_data

angular firebase firebase-realtime-database angularfire2
1个回答
0
投票

过滤 Firebase 实时数据库中的数据分为两步:

  1. 首先,通过调用
    orderByKey
    orderByValue
    orderByChild
    对某个值的数据进行排序。该值必须位于您查询的路径正下方的每个子节点下的固定路径。
  2. 然后您可以过滤您订购的值,例如使用
    equalTo
    操作。

由于您没有调用任何

orderBy
,因此您的节点按其优先级或键排序,并且这些优先级/键都不与您过滤的值匹配。


如果您有一个

ref
指向您共享的 JSON 的根,您可以使用以下方法过滤
name
属性:

ref.orderByChild("name").equalTo("Demo 1")

这将为您提供一个包含一个结果节点的列表;

-NLBD8PD7i5DPfdgF


如果您

ref
指向您共享的 JSON 中的
-NLBD8PD7i5DPfdgF
,您可以使用以下方法在其
name
属性上过滤该特定节点的行:

ref.child("rows").orderByChild("name").equalTo("Data 2")

这将再次为您提供一个包含单个结果节点的列表,即带有键

-NLBPODZMfFFXKTFDo7p
的行。


无法在多个属性中搜索其中任何一个包含该值。如果您需要,请考虑使用专用搜索引擎而不是 Firebase 实时数据库。


无法在整个 JSON 中过滤特定行中的值,因为这不再满足要过滤的值存在于每个 direct 子节点下的固定路径上的要求。

有关该问题的更多信息以及通过更改数据模型可能实现的解决方案,请参阅 Firebase 查询双重嵌套Firebase 查询(如果子级的子级包含值)

© www.soinside.com 2019 - 2024. All rights reserved.