29

I have data set up like this:

Tasks:{
  Group 1:{
    pushid1:{
      date: 18921949021,
      title: "Do something"
    },
    pushid2:{
      date: 18921949021,
      title: "Do something else"
    }
  },
  Group 2:{
    pushid3:{
      date: 18921949021,
      title: "Do another thing"
    }
  }
}

I reference it by date so I only get the tasks made after a certain time.

var now = Date.now();
var taskRef = new Firebase(FB + "/tasks/" + thisGroup);
taskRef.orderByChild('date').startAt(now).on("child_added", function(snap){
  console.log(snap.val());
});

This all works fine, for each group I get a warning message saying something like this:

FIREBASE WARNING: Using an unspecified index.  Consider adding ".indexOn": "date" at tasks/Group 1 to your security rules for better performance

How do I add that to my security rules? I can't add one for each group a user makes, is there a wild card entry of some type?

AL.
  • 36,815
  • 10
  • 142
  • 281
Marty.H
  • 1,194
  • 4
  • 16
  • 29

2 Answers2

58

I guess you're looking for:

{
  ".read": true,
  ".write": true,
  "rules": {
    "Tasks": {
      "$groupid": {
        ".indexOn": "date"
      }
    }
  }
}

The Firebase documentation is usually a pretty good read. Spending an hour or less in the security guide, will ensure you have a better understanding of this and many other topics.

AL.
  • 36,815
  • 10
  • 142
  • 281
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    I'm still getting "FIREBASE WARNING: Using an unspecified index. Consider adding ".indexOn": "clinics/1/date" at /requests to your security rules for better performance " – Steve Nov 17 '16 at 18:16
  • 1
    I got the similar issue with two level on authentication provider (e.g. google, twitter) and the ID (e.g. email). Both I can't hard code, what option do I have to index by the leaf elements, when two level of parent we can't hard code. Appreciate any help – user1687711 Dec 28 '16 at 22:45
  • @marty-h The `$groupid` was the wild card entry that the asker was asking for. – KarolDepka Aug 31 '17 at 14:46
  • Same issue here, did you fix it @Steve – Elyx0 Oct 15 '19 at 19:17
1

I used Frank's answer, but it was giving me error,

Error saving rules - Line 1: Expected 'rules' property.

So, this worked for me:

{
  "rules": {
  ".read": true,
  ".write": true,
    "Tasks": {
      "$groupid": {
        ".indexOn": "date"
      }
    }
  }
}

Screenshot of my Database rules: enter image description here

Madhav Kumar
  • 856
  • 8
  • 19