Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an index on a child property of an array object in Firebase

Tags:

firebase

In my firebase app I am storing objects in an array structure using the push() method that generates unique ids. These objects have a child property called "scheduledDate" How can I create an indexOn rule in firebase to index this field. under events each user has a node, than a child array of their events. /events/userId/arrayOfEvents

I can use orderByChild() to successfully retrieve the records by event scheduled date, but its warning me about the index.

My rules looks like this:

   "events":{
      ".read" : "auth != null",
      ".write" : "auth != null",
      ".indexOn": ["scheduledDate"]
    } 

My code doing the query looks like this:

         this.dbEvents = new Firebase(firebaseEventsUrl);

         var q;

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

         return $firebaseArray(q);

My data looks like this:

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

              }
          }
like image 612
Stradosphere Avatar asked Nov 06 '15 18:11

Stradosphere


1 Answers

This should work:

{
  "rules": {
    "events":{
      ".read" : "auth != null",
      ".write" : "auth != null",
      "$uid": {
        ".indexOn": "scheduledDate"
      }
    } 
  }
}
like image 52
Frank van Puffelen Avatar answered Nov 04 '22 02:11

Frank van Puffelen