如何在Firebase中的数组对象的子属性上创建索引

Str*_*ere 13 firebase

在我的firebase应用程序中,我使用生成唯一ID的push()方法将对象存储在数组结构中.这些对象有一个名为"scheduledDate"的子属性如何在firebase中创建indexOn规则来索引此字段.在事件下,每个用户都有一个节点,而不是他们事件的子数组./事件/用户名/ arrayOfEvents

我可以使用orderByChild()按事件计划日期成功检索记录,但它会警告我索引.

我的规则如下:

   "events":{
      ".read" : "auth != null",
      ".write" : "auth != null",
      ".indexOn": ["scheduledDate"]
    } 
Run Code Online (Sandbox Code Playgroud)

我执行查询的代码如下所示:

         this.dbEvents = new Firebase(firebaseEventsUrl);

         var q;

         q = this.dbEvents.child(userId).orderByChild("scheduledDate").limitToLast(limitToLast);

         return $firebaseArray(q);
Run Code Online (Sandbox Code Playgroud)

我的数据如下:

          events:{
              user1:{
                  -K2NYCT2_uMwsTSfH58O(push generated):{
                      description: 'event 1',
                      scheduledDate: 1446730200000
                  },
                  --K2NyNh660I4k8loar-z:{
                      description: 'event 2',
                      scheduledDate: 1446730200000
                  },
              },
              user2:{

              }
          }
Run Code Online (Sandbox Code Playgroud)

Fra*_*len 26

这应该工作:

{
  "rules": {
    "events":{
      ".read" : "auth != null",
      ".write" : "auth != null",
      "$uid": {
        ".indexOn": "scheduledDate"
      }
    } 
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 这很有效,谢谢弗兰克!可以将"$ uid"命名为表示该级别的变量吗? (2认同)
  • 是的,它只是一个变量名.请阅读以下文档的这一部分:https://www.firebase.com/docs/security/guide/securing-data.html#section-dollar-variables (2认同)