Firebase realtime database : how to check if child value exists

时间:2016-08-31 12:18:16

标签: javascript firebase firebase-realtime-database

Given the document below.

website:{
    uid1:{
        foo:"bar",
        username:"joff"
    },
    uid2:{
        foo:"bar2",
        username:"karla"
    }        
}

I am trying to check if the username already exist using the query below.

var ref = db.ref('website');
var queryString = 'joff';

ref.orderByChild('username').equalTo(queryString).once('value', function(snap){
    if(snap.exists()){
        // exists
        return
    }
    console.log('available');
})

This approach is working without a problem, if I change the queryString accordingly I get the result that I want if it exists or not.

Firebase's default query limit is 100 and I can change it to my liking according to the documentation.

But what if I will query though a let's say 10,000 documents? I cant use paginated query here, because what if the query match is on the 2nd page? How do I handle such amount of data? Will it take too long before it respond? What is the right approach to do such query.

PS

I strictly need the data model to be the same as the given above.

Thanks in advance.

1 个答案:

答案 0 :(得分:1)

我认为使用equalTo会产生一个" smart"查询,因此查询限制不会成为确定记录是否存在的因素。虽然我不是正面的。您是否确实已经确认拥有超过100条记录,并且您的“joff”在排序中处于100+,导致无法匹配?

如果是这种情况(这会非常令人惊讶),那么您可能会重构您的查询:

ref.orderByChild('username').startAt(queryString).endAt(queryString).once('value', function(snap){
    if(snap.exists()){
        // exists
        return
    }
    console.log('available');
})

...但是,我再也不认为在使用equalTo时需要这样做。