Firebase:如何为嵌套字段添加索引?

时间:2016-11-09 16:37:39

标签: firebase firebase-realtime-database firebase-security

我有一个locations-orders表,如下所示:

{
  "0BW3H9T3R7HJB" : {
    "orders" : {
      "01750ea0-4980-4bc4-58b2-988c02324e671478636582396" : {
        "created_at" : 1478636560000,
      },
      "01750ea0-4980-4bc4-58b2-988c02324e671478636582483" : {
        "created_at" : 1478636560000,
      }
    }
}

每个location-orders节点都有一个orders节点,其中包含多个键/对象。这些对象上有created_at字段。

我将此添加到我的数据库规则:

{
  "rules": {
    ".read": true,
    ".write": true,
    "users": {
      ".indexOn": "merchantId"
    },
    "merchants": {
      ".indexOn": "locations"
    },
    "locations-orders": {
      ".indexOn": ["orders/created_at"]
    }
  }
}

然而,Firebase仍在抱怨我错过了一个索引:

Using an unspecified index. Consider adding ".indexOn": "created_at" at /locations-orders/1JS53G0TT5ZQD/orders to your security rules for better performance

我应该运行一些东西来创建索引吗?还是写得不正确?

=== UPDATE ===

我更改了文件以查看:

{
  "rules": {
    ".read": true,
    ".write": true,
    "users": {
      ".indexOn": "merchantId"
    },
    "merchants": {
      ".indexOn": "locations"
    },
    "locations-orders": {
      "$location_id": {
        ".indexOn": ["orders/created_at", "orders/status_updated_at"]
      }
    }
  }
}

但我仍然得到同样的警告:

Using an unspecified index. Consider adding ".indexOn": "created_at" at /locations-orders/1JS53G0TT5ZQD/orders to your security rules for better performance

2 个答案:

答案 0 :(得分:3)

如果查看数据结构,orders/created_at下就没有/location-order/$orderId

{
  "rules": {
    ".read": true,
    ".write": true,
    "users": {
      ".indexOn": "merchantId"
    },
    "merchants": {
      ".indexOn": "locations"
    },
    "locations-orders": {
      "$someid": {
        ".indexOn": ["orders/created_at"]
      }
    }
  }
}

答案 1 :(得分:1)

试试这个:

{
  "rules": {
    ".read": true,
    ".write": true,
    "users": {
      ".indexOn": "merchantId"
    },
    "merchants": {
      ".indexOn": "locations"
    },
    "locations-orders": {
      "orders": {
        "$orderid": {
          ".indexOn": "created_at"
        }
      }
    }
  }
}