忽略Firebase子项

时间:2018-03-19 17:25:17

标签: ios json swift firebase firebase-realtime-database

我怎样才能以这种方式查询这些数据:

我想忽略CurrentSubAdministrativeArea子项并迭代每个子子项并找到正确的userKey

其实我正在使用这段代码,这不起作用:

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"
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

您可以以易于构建和执行查询的方式对数据进行非规范化。

与您已经拥有的数据结构一起,您将拥有另一个节点(即另一个数据结构),如

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

使用NoSQL数据库,您应该毫不犹豫地以这样一种方式复制数据,使您的查询变得简单快捷。