Firebase调用`child(all).equalTo(id)`

时间:2017-08-15 13:47:33

标签: node.js firebase firebase-realtime-database

我想检查包含具有变量id值的子项的所有子项。

这就是我提出的:

usersref.child(formData.openid + '/votes').child().equalTo(formData.appid).once('value', function(snapshot) {});

返回:Firebase.child失败:使用0参数调用。预计至少有1个。

也尝试过:

usersref.child(formData.openid + '/votes').orderByKey().equalTo(formData.appid).once('value', function(snapshot) {});

这不会引发错误,但不会返回任何值。

usersref.child(formData.openid + '/votes').orderByChild('a').equalTo(formData.appid).once('value', function(snapshot) {});
usersref.child(formData.openid + '/votes').orderByChild('b').equalTo(formData.appid).once('value', function(snapshot) {});
usersref.child(formData.openid + '/votes').orderByChild('c').equalTo(formData.appid).once('value', function(snapshot) {});
...
usersref.child(formData.openid + '/votes').orderByChild('z').equalTo(formData.appid).once('value', function(snapshot) {});

这很有效,但需要经过很多请求。

数据结构:

{
  "AzxaXACf7UeOOsmlLh5vp4" : {
    "favorites" : {
      "3811" : true,
      "3910" : true
    },
    "votes" : {
      "-KraGbsn8bHop70uUISB" : {
        "upvote" : "3811"
      },
      "-KraGcxU3gR-pqPUcEs8" : {
        "downvote" : "3811"
      },
      "-KraGkJp4oGivIF9qjml" : {
        "upvote" : "3910"
      },
      "-KraGrOy156zy5pXoR-G" : {
        "upvote" : "3903"
      },
      "-KraHhNKp2ISqNeYzS2g" : {
        "downvote" : "3903"
      },
      "-KraHr2ZANh6FlZ3xn4T" : {
        "upvote" : "3280"
      },
      "-KraKLTqwli9Q7j8mQ8w" : {
        "upvote" : "3653"
      },
      "-KraM0qUqeu9tyeHyesE" : {
        "downvote" : "3910"
      },
      "-KraM5MfgO-WW1E-aZK_" : {
        "downvote" : "2858"
      },
      "-KraMVvGSeDwkUr_2ece" : {
        "downvote" : "2411"
      },
      "-KraMWF-4oAycb0Gg3sH" : {
        "upvote" : "2411"
      },
      "-KraMifSigQ06jFvG0Q_" : {
        "upvote" : "2344"
      }
    }
  },
  "CLsAiPzci5Re0A5RvchUaA" : {
    "favorites" : {
      "3811" : true,
      "3884" : true,
      "3970" : true
    },
    "votes" : {
      "-Kra4o-J3vEWBxWUDvBO" : {
        "downvote" : "3849"
      },
      "-Kra4tV883O2N6_TZ_-P" : {
        "upvote" : "3884"
      },
      "-Kra4wY5jfbnm1xod-lU" : {
        "downvote" : "3884"
      },
      "-Kra54vEXVgk6Yksw_i6" : {
        "upvote" : "3778"
      },
      "-Kra5CS3iYrskVH8zBMi" : {
        "upvote" : "3764"
      }
    }
  },
  "ChzvupeGjiG0BYmCHinJrE" : {
    "favorites" : {
      "3811" : true,
      "3910" : true
    },
    "votes" : {
      "-KraFNIoZpFqHvvOtFXh" : {
        "downvote" : "3811"
      },
      "-KraFQ_Ym9g9Jplxx1On" : {
        "upvote" : "3811"
      },
      "-KraFcEFNIVTfu8VkHGJ" : {
        "upvote" : "3910"
      },
      "-KraFfawEo7Km6jn_dk5" : {
        "downvote" : "3678"
      },
      "-KraFlSGxJl_5owfGsZd" : {
        "downvote" : "3778"
      },
      "-KraFpsRnEl4CjQN217w" : {
        "downvote" : "3769"
      }
    }
  },
  "I1SKo3UK8BmeET9fPPvpuo" : {
    "votes" : {
      "-KraFMANOoLdOFoICWH2" : {
        "upvote" : "3910"
      }
    }
  },
  "K_a5kz2pdgYfOiBmT8JGxc" : {
    "votes" : {
      "-Kracl3JaWWYeD_YLVan" : {
        "downvote" : "3764"
      }
    }
  },
  "LogzS_ZExtPm8AV6YolLkM" : {
    "favorites" : {
      "3811" : true,
      "3970" : true
    },
    "votes" : {
      "-Kra1HNVSRYnGOMq0xZK" : {
        "downvote" : "3970"
      },
      "-Kra1OWl52FfUehK7-yq" : {
        "upvote" : "3970"
      },
      "-Kra1ZLVzxo4TtXquaZu" : {
        "upvote" : "3851"
      },
      "-Kra1bKIWj0anYj-F4sr" : {
        "downvote" : "3851"
      },
      "-Kra1flJIk6CzT6BW2di" : {
        "upvote" : "3778"
      },
      "-Kra1sU1FpSPxCSH-NkC" : {
        "upvote" : "3903"
      },
      "-Kra1wxxyDNQqZCYI0ZO" : {
        "upvote" : "3811"
      }
    }
  },
  "OufAmSLsiabJc0ou2oqA2c" : {
    "favorites" : {
      "2896" : true
    },
    "votes" : {
      "-Kra4zr7J5RFv8X96BPp" : {
        "upvote" : "3903"
      },
      "-Kra5-mUpIDqiV-xbm5e" : {
        "downvote" : "3903"
      },
      "-Kra50ZODLPN-Hz_M3_P" : {
        "upvote" : "3903"
      },
      "-Kra5OuuuqmIiHBf0kut" : {
        "upvote" : "3903"
      },
      "-Kra5PqL4H0oN-MSSNjr" : {
        "upvote" : "3903"
      },
      "-KradBD0hRetdaq7ia0Q" : {
        "downvote" : "2896"
      },
      "-KradCiHyImnvRy-Uzu3" : {
        "upvote" : "2896"
      },
      "-Kradu_d25JMX69TOAVB" : {
        "upvote" : "3811"
      },
      "-KrakdW3B7rSg3Fhr2o-" : {
        "upvote" : "3778"
      },
      "-KrakgwUjMDIHPpL2wh2" : {
        "upvote" : "3885"
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

没有Firebase Database API可以搜索所有子项以获取特定值。

我能想到的最好的方法是将所有值的倒排索引创建回任何孩子中具有该值的投票。所以:

app_index
  appid1
    voteid1: true
    voteid2: true
    voteid3: true
  appid2
    voteid1: true
    voteid3: true
  appid3
    voteid2: true
    voteid4: true

通过此功能,您可以查找特定应用ID的所有选票。