14

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:{

              }
          }
Stradosphere
  • 1,285
  • 3
  • 14
  • 40
  • Descriptions of data or code are not the same as actual data/code. Please show a sample of your data (no screenshot please) and the code that you use to query it. – Frank van Puffelen Nov 06 '15 at 18:53
  • edited to show code and data structure I am working with. Again, it works, just warns me I need to index scheduledDate, and I don't know how to create that indexOn rule for a level this deep. – Stradosphere Nov 06 '15 at 22:19

1 Answers1

28

This should work:

{
  "rules": {
    "events":{
      ".read" : "auth != null",
      ".write" : "auth != null",
      "$uid": {
        ".indexOn": "scheduledDate"
      }
    } 
  }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807