Abhishek Batra
Abhishek Batra

Reputation: 1599

Firebase Database: Get only one child node, out of many child nodes

I am using firebase real-time database. I don't want to get all child nodes for a particular parent node, I am concerned this with a particular node, not the sibling nodes. Fetching all the sibling nodes increases my billing in firebase as extra XXX MB of data is fetched. I am using NodeJs admin library for fetching this.

Adding a sample JSON

{
  "phone" : {
    "shsjsj" : {
      "battery" : {
        "isCharging" : true,
        "level" : 0.25999999046325684,
        "updatedAt" : "2018-05-15 12:45:29"
      },
      "details" : {
        "deviceCodeName" : "sailfish",
        "deviceManufacturer" : "Google",
        "deviceName" : "Google Pixel",
      },
      "downloadFiles" : {
        "7bfb21ff683f8652ea390cd3a380ef53" : {
          "uploadedAt" : 1526141772270,
        }
      },
      "token" : "cgcGiH9Orbs:APA91bHDT3mI5L3N62hqUT2LojqsC0IhwntirCd6x0zz1CmVBz6CqkrbC",
      "uploadServer" : {
        "createdAt" : 1526221336542,
      }
    },
    "hshssjjs" : {
      "battery" : {
        "isCharging" : true,
        "level" : 0.25999999046325684,
        "updatedAt" : "2018-05-15 12:45:29"
      },
      "details" : {
        "deviceCodeName" : "sailfish",
        "deviceManufacturer" : "Google",
        "deviceName" : "Google Pixel",
      },
      "downloadFiles" : {
        "7bfb21ff683f8652ea390cd3a380ef53" : {
          "uploadedAt" : 1526141772270,
        }
      },
      "token" : "cgcGiH9Orbs:APA91bH_oC18U56xct4dRuyw9qhI5L3N62hqUT2LojqsC0IhwntirCd6x0zz1CmVBz6CqkrbC",
      "uploadServer" : {
        "createdAt" : 1526221336542,
      }
    }
  }
}

In the above sample JSON file, i want to fetch all phone->$deviceId->token. Currently, I am fetching the whole phone object, then I iterate over all the phone ID's to fetch the token. This spikes my database download usage and increases billing. I am only concerned with the token of all the devices. Siblings of the token is unnecessary.

Upvotes: 0

Views: 384

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

All queries to Realtime Database fetch everything under the location requested. There is no way to limit to certain children under that location. If you want only certain children at a location, but not everything under that location, you'll have to query for each one of them separately. Or, you can restructure or duplicate your data to support the specific queries you want to perform - duplication is common for nosql type databases.

Upvotes: 2

Related Questions