0

I was trying to get a book value using JavaScript and storing in a variable named "bookName" now it has a decimal value 124562. This decimal value indicates some book details.

Now the problem is in firebase I made a data modeling a node named as "books" and in there there are different details like:

Book name : tcp/ip

Book number : 124562

Book pages: 300

The problem is how to get the query in firebase that it will compare different books under books node and get me the details of the book number 124562?

Your help in the form of video or event syntax will be helpful.

Also I checked , the Syntax

UserRef.orderByChild('books').equalTo(value).on('child-added',function(snap){
console.log(snap.val());
)};
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • say my user is getting some rf id card scanned , and the value of book is in the var (javascript), although i upload on firebase creating a child, the thing is i want dynamic, that if this book then so this, .. eg, if tcp/ip then show all details "" – Siddesh Pawar Mar 20 '18 at 17:49

2 Answers2

0

in firebase query you can query based on one parameter at one time so if you want to say get the books whose name start with "tcp" you can query like this

userRef.child("books").orderByChild("name")
    .startAt(queryText).endAt(queryText+"\uf8ff").once("value")

Incase of book pages:

userRef.child("books").orderByChild("name")
        .startAt(fromPageNumber).endAt(toPageNumber).once("value")

here is similar answer as well : How to do a simple search in string in Firebase database?

See the documentation:

https://firebase.google.com/docs/database/admin/retrieve-data#section-queries

Incase of more complex queries like having multiple parameters in your query, you need to use third party solution like Elastic Search.

Umar Hussain
  • 3,461
  • 1
  • 16
  • 38
0

Say you have this JSON under /books in your database:

"books": {
  "-Lsdfkfdskgdskg1": {
    "name": "tcp/ip"
    "number": 124562
    "pages": 300
  },
  "-Lsdfkfdskgdskg1": {
    "name": "some other name"
    "number": 987654
    "pages": 120
  }
}

You can query it with:

var booksRef = firebase.database().ref("books");
booksRef.orderByChild('number').equalTo(124562).on('child_added',function(snap){
    console.log(snap.val());
)};

The changes from your code:

  • Create a reference to all books, then order by and filter on number
  • It's child_added with an _

Note that it seems your books already have their own unique ID. In that case, it's more efficient to store the books under that ID in the JSON:

"books": {
  "124562": {
    "name": "tcp/ip"
    "pages": 300
  },
  "987654": {
    "name": "some other name"
    "pages": 120
  }
}

Now you can load the book without needing a query:

booksRef.child("124562").on('value',function(snap){
    console.log(snap.val());
)};

Queries get slower as you add more books. But with this JSON structure you can directly access the data for the book based on its ID, which scales much better.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • booksRef.child("124562").on('value',function(snap){ console.log(snap.val()); )}; here it seems you are predetermining the value to be "124562" say a user needs another one , i.e 987654 – Siddesh Pawar Mar 20 '18 at 17:50