0

enter image description here

Hi i want to do something like, when theres is data changes within the "Always" under parent Rooms, those data changes must be simillar to respective customers child "Always".

Im not sure how to implement it as how to go through each of every child node of CUstomerBookedRooms which are those respective Customers then check if this customer have "Always" under their respective parents node.

This my JSON file:

{
  "Customer" : {
    "johnny123" : {
      "email" : "johnny@gmail.com",
      "fullname" : "johnny",
      "password" : "Abcbc12345",
      "username" : "johnny123"
    },
    "tjing123" : {
      "email" : "jh@gmail.com",
      "fullname" : "JingHuan",
      "password" : "1",
      "username" : "tjing123"
    }
  },
  "CustomerBookedRooms" : {
    "johnny123" : {
      "Always" : {
        "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/finalyearprojectapplicat-8a301.appspot.com/o/RoomImageStorage%2F20476d50-0256-4637-a85f-88cfb0d49509%2Fcontent%3A%2Fcom.android.providers.media.documents%2Fdocument%2Fimage%253A76555?alt=media&token=9042d0c2-1203-48a9-85e0-66de5c30eb84",
        "roombedcounts" : "One Bed",
        "roomcapacity" : "1 pax",
        "roomcharacteristics" : "Cozy",
        "roomname" : "Always",
        "roomprice" : "588.00",
        "roomstatus" : "Available",
        "roomtype" : "Single Room",
        "username" : "johnny123"
      }
    },
    "tjing123" : {
      "Always" : {
        "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/finalyearprojectapplicat-8a301.appspot.com/o/RoomImageStorage%2F20476d50-0256-4637-a85f-88cfb0d49509%2Fcontent%3A%2Fcom.android.providers.media.documents%2Fdocument%2Fimage%253A76555?alt=media&token=9042d0c2-1203-48a9-85e0-66de5c30eb84",
        "roombedcounts" : "One Bed",
        "roomcapacity" : "1 pax",
        "roomcharacteristics" : "Cozy",
        "roomname" : "Always",
        "roomprice" : "588.00",
        "roomstatus" : "Available",
        "roomtype" : "Single Room",
        "username" : "tjing123"
      },
      "The Nuts" : {
        "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/finalyearprojectapplicat-8a301.appspot.com/o/RoomImageStorage%2Fd0f0279a-121b-4747-bdf0-76c9e7bfb30c%2Fcontent%3A%2Fcom.android.providers.media.documents%2Fdocument%2Fimage%253A76556?alt=media&token=e234d404-adfd-4a64-b180-50408c90a823",
        "roombedcounts" : "One Bed",
        "roomcapacity" : "2 pax",
        "roomcharacteristics" : "Cozy",
        "roomname" : "The Nuts",
        "roomprice" : "258.00",
        "roomstatus" : "Available",
        "roomtype" : "Master Room",
        "username" : "tjing123"
      }
    }
  },
  "Rooms" : {
    "Always" : {
      "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/finalyearprojectapplicat-8a301.appspot.com/o/RoomImageStorage%2F20476d50-0256-4637-a85f-88cfb0d49509%2Fcontent%3A%2Fcom.android.providers.media.documents%2Fdocument%2Fimage%253A76555?alt=media&token=9042d0c2-1203-48a9-85e0-66de5c30eb84",
      "roombedcounts" : "One Bed",
      "roomcapacity" : "1 pax",
      "roomcharacteristics" : "Cozy",
      "roomid" : "1111",
      "roomname" : "Always",
      "roomprice" : "588.00",
      "roomstatus" : "Available",
      "roomtype" : "Single Room"
    },
    "HEHE" : {
      "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/finalyearprojectapplicat-8a301.appspot.com/o/RoomImageStorage%2F36b436d2-0897-41a3-9d08-0ec48ec13a5d%2Fcontent%3A%2Fcom.android.providers.media.documents%2Fdocument%2Fimage%253A25?alt=media&token=2984d69a-b52c-4003-9adc-eb7556b97bf7",
      "roombedcounts" : "Three Beds",
      "roomcapacity" : "2 pax",
      "roomcharacteristics" : "Scenery View",
      "roomid" : "1111",
      "roomname" : "HEHE",
      "roomprice" : "399.00",
      "roomstatus" : "Unavailable",
      "roomtype" : "Master Room"
    },
    "Huan&Jian" : {
      "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/finalyearprojectapplicat-8a301.appspot.com/o/RoomImageStorage%2Fd8ad8454-29b4-4a44-b2c0-4da584c0cf17%2Fcontent%3A%2Fcom.android.providers.media.documents%2Fdocument%2Fimage%253A71334?alt=media&token=0cbd8564-adbd-4a69-8bbb-4c0b8af1ab5c",
      "roombedcounts" : "One Bed",
      "roomcapacity" : "2 pax",
      "roomcharacteristics" : "Cozy",
      "roomid" : "1314",
      "roomname" : "Huan&Jian",
      "roomprice" : "188.00",
      "roomstatus" : "Maintenance",
      "roomtype" : "Couple Room"
    },
    "Kokomi" : {
      "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/finalyearprojectapplicat-8a301.appspot.com/o/RoomImageStorage%2F175ac041-af4d-4ffd-8b0f-0f04f3b293f5%2Fcontent%3A%2Fcom.google.android.apps.photos.contentprovider%2F0%2F1%2Fmediakey%253A%252Flocal%25253Ab456c215-0536-4e05-8f77-616f83d36055%2FORIGINAL%2FNONE%2F1931479739?alt=media&token=1cab4f94-929f-4f52-8a46-aa76c82a0848",
      "roombedcounts" : "Two Beds",
      "roomcapacity" : "2 pax",
      "roomcharacteristics" : "Cozy",
      "roomid" : "5555",
      "roomname" : "Kokomi",
      "roomprice" : "499.00",
      "roomstatus" : "Maintenance",
      "roomtype" : "Couple Room"
    },
    "The Nuts" : {
      "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/finalyearprojectapplicat-8a301.appspot.com/o/RoomImageStorage%2Fd0f0279a-121b-4747-bdf0-76c9e7bfb30c%2Fcontent%3A%2Fcom.android.providers.media.documents%2Fdocument%2Fimage%253A76556?alt=media&token=e234d404-adfd-4a64-b180-50408c90a823",
      "roombedcounts" : "One Bed",
      "roomcapacity" : "2 pax",
      "roomcharacteristics" : "Cozy",
      "roomid" : "7272",
      "roomname" : "The Nuts",
      "roomprice" : "258.00",
      "roomstatus" : "Available",
      "roomtype" : "Master Room"
    }
  },
  "Staff" : {
    "tt" : {
      "email" : "raymondting521@gmail.com",
      "fullname" : "raymondting",
      "password" : "1",
      "username" : "tt"
    }
  }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Raymond Jian
  • 105
  • 7

1 Answers1

0

There is no way to search for that Always key under all CustomerBookedRooms child nodes.

Your current data model makes it easy to find all the rooms that a specific customer booked, but not all customers that booked a specific rooms.

To allow that use-case you will need to add another top-level data structure, which is essentially the opposite of your CustomerBookedRooms branch, mapping from each room back the customers that booked it.

RoomsToCustomer: {
  "Always": {
    "johnny123": true,
    "tjing123": true
  },
   "The Nuts": {
    "tjing123": true
  }
}

With this additional data structure, you can easily look up the customers for a given room id, and from that result then update them under the CustomerBookedRooms node.

This is pretty common when you have a bi-directional relationship in a NoSQL database, so I recommend checking out my answers to these related questions:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks for the answer @Frank van Puffelen, to clarify, i will need to add a table with parents name RoomsToCustomer with the child of all the "roomnames" and whenever one customer decided to book that specific room, lets say the customer wants to book the room "Always", then i just assigned the customer name to "true" under that RoomToCustomer specific roomnames? I think i have an idea here, is this what u mean? – Raymond Jian Jan 27 '22 at 04:55
  • 1
    The name of the new branch doesn't really matter. The important thing is that under it you first have the room keys, and then under that the user keys for that room. I used `true` here as a marker, since a key won't exist until it has a value. The actual value doesn't really matter, so I used true, but there are also plenty of other scenarios where you use a timestamp as the value, so that you order them on e.g booking date, or where you duplicate the entire set of properties from the equivalent `CustomerBookedRooms` child node. – Frank van Puffelen Jan 27 '22 at 05:20
  • Thank you so much sir, i think i get what uve meant! – Raymond Jian Jan 27 '22 at 06:47
  • Sir is this the right way to check? if(snapshot.child("Always").child(johnny123).equals(true)){} – Raymond Jian Jan 27 '22 at 07:32