Fred37b
Fred37b

Reputation: 854

Angular Firebase How to find and retrieve data by field

I have datas on realtime Database on firebase with the following structure :

    {   "tests" : [ {
           "description" : "55",
           "number" : "11",
           "serie" : "0"   }, {
           "description" : "22",
           "number" : "13",
           "serie" : "0"   }, {
           "description" : "Test",
           "number" : "55",
           "serie" : "2"   
} ]

And I want tro retrieve all my data where the field "serie" are equal to "0". I have a methode in my service component for retrieving all the tests data, like this :

  getTests(){
    firebase.database().ref('/tests')
      .on('value',(data: DataSnapshot)=>{
        this.tests= data.val() ? data.val():[];
        this.emitTests;
      }
    );
  }

At this time I do not know how retrieving only datas where serie is equal to 0.

Upvotes: 1

Views: 1845

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599776

You do that by using a Firebase Database query:

firebase.database().ref('/tests')
  .orderByChild("serie").equalTo("0")
  .on('value',(data: DataSnapshot) => {
    data.forEach((child: DataSnapshot) => {
      console.log(child.key, child.val());
    });

As you can see a query in Firebase has two steps:

  1. You order the data, either on a child property or on its key of value.
  2. You then filter the data based on what you ordered on.

Also see the Firebase documentation on ordering and filtering data.

Upvotes: 2

Related Questions