0

My firebase data looks like this...

firebase database

In JSON down below...

I've been trying to use what I've found in their docs to query the db so that I end up with of all the terms and their keys. How do I query just the term property and key for every node under vocab? I'm using this to populate an autosuggest. Also, the 0 and 1 under the vocab node are keys... they showed up that way when I imported the data as a JSON file. Any new vocab goes in with unique long key values though.

I've tried both .on() and .equalTo()... But I have had no luck whatsoever. It always seems to be grabbing everything in the entire vocab node of the database and taking on the order of 7 whole seconds to do it each time. Here is an example of one of my attempts where I try to put just the terms into an array...

let termList = [];
const termsRef = firebase.database().ref('vocab/');
termsRef.on('child_added', snap => termList.push(snap.val().term));

This doesn't even get the keys (which I need) and it still takes around 7 seconds to do it. I've seen videos where people do things like this in one line and it goes quick... not my luck. Any help would be much appreciated.

As requested here is a JSON sample of one of the database nodes... there are over 400 like this... the relevant property is term... all the way at the bottom. I need an array of terms and firebase database keys. How do I get them with a query?

{
    "date" : "2016-07-26T14:50:10.906Z",
    "defs" : [ {
      "AddedBy" : "",
      "date" : "2016-07-26T14:50:10.906Z",
      "def" : "it's your need to fulfill or to obtain your full potential, and have meaningful goals.",
      "image" : ""
    }, {
      "AddedBy" : "",
      "date" : "2016-07-26T14:50:10.906Z",
      "def" : "the very last stage in priority in order to be happy.",
      "image" : ""
    }, {
      "AddedBy" : "",
      "date" : "2016-07-26T14:50:10.906Z",
      "def" : "the need to be able to set goals and reach them and be able to accept one's self.",
      "image" : ""
    } ],
    "examples" : [ {
      "AddedBy" : "",
      "addDate" : "2016-07-26T14:50:10.906Z",
      "approved" : true,
      "checkDate" : "2016-07-26T14:50:10.906Z",
      "example" : "a self-actualizing person is a person who accepts themselves, sets realistic and meaningful goals and accepts change.",
      "feedback" : {
        "comment" : "",
        "grammarTrouble" : false,
        "incorrect" : false,
        "moreSpecific" : false,
        "noContext" : false
      },
      "nonExample" : false,
      "seenRecently" : [ {
        "sawOn" : "",
        "user" : ""
      } ]
    }, {
      "AddedBy" : "",
      "addDate" : "2016-07-26T14:50:10.906Z",
      "approved" : true,
      "checkDate" : "2016-07-26T14:50:10.906Z",
      "example" : "who is not self-actualizing is a person who dislikes themselves, has no goals in life, and refuses to accept change.",
      "feedback" : {
        "comment" : "",
        "grammarTrouble" : false,
        "incorrect" : false,
        "moreSpecific" : false,
        "noContext" : false
      },
      "nonExample" : false,
      "seenRecently" : [ {
        "sawOn" : "",
        "user" : ""
      } ]
    }, {
      "AddedBy" : "",
      "addDate" : "2016-07-26T14:50:10.906Z",
      "approved" : true,
      "checkDate" : "2016-07-26T14:50:10.906Z",
      "example" : "Someone who achieves to their best personal ability and faces every situation head on.",
      "feedback" : {
        "comment" : "",
        "grammarTrouble" : false,
        "incorrect" : false,
        "moreSpecific" : false,
        "noContext" : false
      },
      "nonExample" : false,
      "seenRecently" : [ {
        "sawOn" : "",
        "user" : ""
      } ]
    } ],
    "term" : "Self Actualization",
    "unit" : 0
  },
Serg Chernata
  • 12,280
  • 6
  • 32
  • 50
Brad Wray
  • 73
  • 1
  • 7
  • It's somewhat unclear to me what you're asking. Can you show what type of query you tried and what result you're getting (vs what you expected)? Also: please replace the screenshot with the actual JSON as text, which you can easily get by clicking the Export JSON link in [your Firebase Database console](https://console.firebase.google.com/project/_/database/data/). Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. – Frank van Puffelen Dec 30 '16 at 17:25
  • I made some changes. I can't put all 400 JSON objects in that post through. Anyhow, the type of query I'd like to do is to retrieve all of the values of "term" and the firebase db keys to which they belong. But nothing else. The example that I posted using .on() seems to do a lot more than I need it to since it takes so long. I assume that the .on() example returns a snapshot of everything... not just the terms and their keys. I'd like to be able to just go in and grab the value of term and the firebase db keys for all of the terms. – Brad Wray Dec 30 '16 at 18:00
  • This looks like a JSON model that will not be efficiently queryable with Firebase's query capabilities. Consider separating the parts that you want to query into their own node. So in your case that would be a map of keys-and-terms. That will be efficient to query (and index) and then you can look up the actual items based on their keys. – Frank van Puffelen Dec 30 '16 at 18:08
  • For an example (that would allow for multiple terms per key), see my answer here: http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Dec 30 '16 at 18:10
  • Ok, I began doing that a while ago but I gave up on it thinking that there had to be a way to just query the relevant data. Is this technique of making a separate node of the relevant data called denormalization? Thanks! – Brad Wray Dec 30 '16 at 18:26
  • It's one form of denormalizing: creating a custom index for your searches. There are many other parts to it. I recommend reading [NoSQL data modeling](https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/) and viewing [Firebase for SQL developers](https://www.youtube.com/playlist?list=PLl-K7zZEsYLlP-k-RKFa7RyNPa9_wCH2s). As it stands to your original question: by far the most common factor in the time it takes to get data from Firebase is the amount of data you're downloading. But it's hard to be more specific, without seeing the problem reproduced. – Frank van Puffelen Dec 30 '16 at 21:32

0 Answers0