Federic
Federic

Reputation: 91

Ignoring Firebase child

How could I query this data in this way:

I would like to ignore the CurrentSubAdministrativeArea child and iterate every each sub child and find the right userKey

Actually I'm using this code, that isnt working:

self.ref.child("Ads").child("CurrentSubAdministrativeArea")
/*HERE I would like to ignore the childs*/
.queryOrdered(byChild: "userKey").queryEqual(toValue: uid).observeSingleEvent(of:.value, with: { (snapshot) in

--

{
  "Ads" : {
    "CurrentSubAdministrativeArea" : {
      "Mantova" : {
        "-L7ymBmmbHkNfhBRte9F" : {
          "cost" : 200,
          "date" : 1527256922000,
          "info" : "Test",
          "maxLimit" : 100,
          "minLimit" : 10,
          "personBadType" : [ "abitudinaria", "antipatica" ],
          "personGoodType" : [ "simpatica", "felice" ],
          "subAdministrativeArea" : "Mantova",
          "title" : "Mantova Test",
          "url" : "https://firebasestorage.googleapis.com/v0/b/team-34540.appspot.com/o/Mantova%20Test?alt=media&token=3a81ed1c-ecd6-4dc0-bd7c-45e093ce8188",
          "userKey" : "OsJRc98sqxPx70iqxFtoqerMzHH2",
          "via" : "viale dei test"
        }
      },
      "Milano" : {
        "-L6qywMC6nxi0fJNMHba" : {
          "cost" : 454,
          "date" : 1528298580000,
          "info" : "Di pollo",
          "maxLimit" : 100,
          "minLimit" : 10,
          "personBadType" : [ "abitudinaria", "antipatica" ],
          "personGoodType" : [ "simpatica", "felice" ],
          "subAdministrativeArea" : "Milano",
          "title" : "Pollo 2",
          "url" : "https://firebasestorage.googleapis.com/v0/b/team-34540.appspot.com/o/Pollo?alt=media&token=fc6a3ec8-5f9a-4347-bdad-2d9715af784d",
          "userKey" : "OsJRc98sqxPx70iqxFtoqerMzHH2",
          "via" : "viale test"
        }
      }
    }
  }
}

Upvotes: 0

Views: 70

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83103

You could denormalize your data in such a way your query is easy to build and execute.

Together with the data structure you already have you would have another node (ie. another data structure) like

{
  "AdsByUsers" : {
    "OsJRc98sqxPx70iqxFtoqerMzHH2": {
      "Mantova",
      "Milano",
      ...
    },
    "abcde88qxPx70iqxFtoqerMzKh5": {
      "Firenze",
      ...
   }

With NoSQL database you should not hesitate to duplicate data in such a way your queries are easy and fast to execute.

Upvotes: 1

Related Questions