FabricioG
FabricioG

Reputation: 3320

Query similar to a where clause in RD

I have the following structure in Realtime Database:

enter image description here

I've read a few questions on SO that it's possible and some say it's not possible so I'm a little confused on if this can be done or not but here goes.

I have multiple members (about 200) and I would like to return only those that are mStatus == true.

I attempted it by doing this:

const dbRef = ref(db, `organization/${org}/members`);
const queryConstraints = [orderByChild("mStatus"), equalTo("value")]
get(query(dbRef, ...queryConstraints)).then((snapshot) => {
  if(snapshot.exists()) {
    console.log(snapshot.val());
  } else {
    console.log('No data available');
  }
})

The return is null here is the security rule I use:

 "rules": {
    ".read": true,
    ".write": true,
      "members": {
        ".indexOn": ["mStatus"]
      }
  }

How would I run this query?

Upvotes: 0

Views: 36

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599726

Since in the comments you said that your members list is under /organization/{uid}/, that is also where you have to define the index in your rules:

{
  "rules": {
    ...
    "organization": {
      "$uid": {
        "members": {
          ".indexOn": ["mStatus"]
        }
      }
    }
  }
}

Upvotes: 1

Related Questions