How does .indexOn works with users and unique keys?

て烟熏妆下的殇ゞ 提交于 2021-02-15 07:49:04

问题


I am confused with index rules in firebase. I have this database and I wanted to extract distance_traveled in battery_and_cables. The thing is, 1 is something like a userID set by the user itself so that will vary.

I used this code in pyrebase to extract the distance_traveled

db.child("mileage_maintenance").order_by_child("distance_traveled").get()

but I get

Index not defined, add ".indexOn": "distance_traveled", for path "/mileage_maintenance", to the rules

This is my database:

This is my database

Any my rules:

{
  "rules": {
    ".read": true,
    ".write": true,
    "mileage_maintenance": {
      "battery_and_cables":{
        ".indexOn": ["distance_traveled"]
       }
    },
  }
}

Is there any way you can go through the 1 and the push key to acquire the distance_traveled?


回答1:


Firebase Realtime Database queries work on a flat list of the nodes under the location where you run the query. So in your case, the query considers the direct children of the mileage_maintenance node (which is just the node 1 in the screenshot).

You can have wildcards in your rules, like in your case:

{
  "rules": {
    ".read": true,
    ".write": true,
    "mileage_maintenance": {
      "$id": {
        "battery_and_cables":{
          ".indexOn": ["distance_traveled"]
         }
      }
    }
  }
}

As you can see, I added a $id level here for the 1 node. That now means that you have an index defined on each /mileage_maintenance/$id/battery_and_cables node for the distance_traveled property of each child node under that.

So with this index, you can query a specific /mileage_maintenance/$id/battery_and_cables path for the distance_traveled values. But you can't run a query on just /mileage_maintenance for all distance_traveled values under it.

If you need such a query, you will need to modify/augment your data model to allow it. For example, by introducing a flat list of distance_traveled nodes that you can then query:

"distance_traveled": {
   "1000000": "1/battery_and_cables/-M01CT...1hMj"
}

Or

"distance_traveled": {
   "1~battery_and_cables~-M01CT...1hMj": 1000000
}

Where the ~ in that last model is just a separate for the segments in that key, since / isn't allowed.

Also see:

  • Firebase Query Double Nested
  • Firebase query if child of child contains a value


来源:https://stackoverflow.com/questions/60221546/how-does-indexon-works-with-users-and-unique-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!