querying nested data in firebase
firebase
Solution
If your question is "how can I get an account based on its email?", then:
var ref = new Firebase("https://your.firebaseio.com/accounts");
ref.orderByChild('email').equalTo('test2@test.com').on('value', function(snapshot) {
console.log(snapshot.val());
});
See this jsbin for a working sample: http://jsbin.com/yikecu/1/edit?js,console
Update
It turns out OP has a nested collection of email addresses for each account and want to select items based on the presence of one email addresses.
var data = {
"accounts" : {
"simplelogin:001" : {
"emails" : ["test1@test.com", 'test1@bla.com'],
"mobile" : "+15555551001"
},
"simplelogin:002" : {
"emails" : ["test2@test.com", 'test2@bla.com'],
"mobile" : "+15555551001"
},
"simplelogin:003" : {
"emails" : ["test3@test.com", 'test3@bla.com'],
"mobile" : "+15555551003"
}
}
};
I indeed don't think that a "contains" query is currently possible. So creating an index yourself is then the best approach. I would keep that index extremely simple:
email_to_uid:
test1@test.com: simplelogin:0001
test1@bla.com: simplelogin:0001
test2@test.com: simplelogin:0002
test3@bla.com: simplelogin:0002
test3@test.com: simplelogin:0003
test3@bla.com: simplelogin:0003
This index assumes that each email address maps to at most one account. If that does not fit your use-case, you'll need to modify the data structure accordingly.
Problem
Now that the new query capabilities are out, I'm going to ask this question. It's similar to other questions where the answers were based on the old API. Example Data : ` { "accounts" : { "simplelogin:001" : { "email" : "test1@test.com", "mobile" : "+15555551001", ... many more fields }, "simplelogin:002" : { "email" : "test2@test.com", "mobile" : "+15555551001", ... many more fields }, "simplelogin:003" : { "email" : "test3@test.com", "mobile" : "+15555551003", ... many more fields } } ` I was hoping the new query capabilites would allow querying nested data. However, based on what I see, in order to find the account with the email address "test2@test.com", I really have to do one of two things: - Do an `.on` for the accounts collection and then filter the results myself. - Create a reference table that has records like below and then query on the reference table to get the link to the accounts table. ` [ { "account": "simplelogin:001", "email": "test1@test.com", "mobile": "+15555551001" }, { "account": "simplelogin:002", "email": "test2@test.com", "mobile": "+15555551001" }, { "account": "simplelogin:003", "email": "test3@test.com", "mobile": "+15555551003" } ] ` Are these really my only options? Is it not possible to use the new query API to query deeper into nested info?