0

I am trying to retrieve data from firebase in such a way that I can count the no of devices (child node inside rooms key) which are turned on (value of child "status" == true).But I am not able to think a optimized solution for this. I tried retrieving data as map and then iterating each key value to find the count but it will be very complex and data consuming.

Is there any way like Querying or something which can do this task.
My database :

{
  "users" : {
    "ct7H9vr5XcSJo1fALsM0QCqGkDl2" : {
      "child account" : {
        "VLFgvcXYm6Mj3KEDpqjbrQjyB9V2" : true,
        "dWzF1xRDymefLEDTNP32exFxoWT2" : true
      },
      "rooms" : {
        "fvyvybuwe12445" : {
          "Devices" : {
            "-MnVQp2bZNjGmgmjKnfI" : {
              "device name" : "A",
              "device type" : "Light",
              "pos" : 1,
              "status" : true,
              "switch no" : 3
            }
          },
          "room name" : "my living room",
          "room type" : 2
        },
        "ghjnnh543456" : {
          "Devices" : {
            "-MnR0TSC8J__sVtqwmr9" : {
              "device name" : "A",
              "device type" : "Fan",
              "pos" : 1,
              "speed" : 2,
              "status" : true,
              "switch no" : 4
            }
          },
          "room name" : "my dining room",
          "room type" : 0
        }
      }
    }
  }
}
Ayush Yadav
  • 366
  • 1
  • 11
  • Firebase queries work on a flat list of node, where the value to order/filter on is at a fixed path under each direct child node. There is no way to query the structure you have across all user and their devices. See https://stackoverflow.com/questions/27207059/firebase-query-double-nested. If you need this as a query, you'll need to (also) store the devices as a flat list. – Frank van Puffelen Nov 02 '21 at 17:22

0 Answers0