Reputation: 6215
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" : "[email protected]",
"mobile" : "+15555551001",
... many more fields
},
"simplelogin:002" : {
"email" : "[email protected]",
"mobile" : "+15555551001",
... many more fields
},
"simplelogin:003" : {
"email" : "[email protected]",
"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 "[email protected]", I really have to do one of two things:
.on
for the accounts collection and then filter the results
myself.
[
{
"account": "simplelogin:001",
"email": "[email protected]",
"mobile": "+15555551001"
},
{
"account": "simplelogin:002",
"email": "[email protected]",
"mobile": "+15555551001"
},
{
"account": "simplelogin:003",
"email": "[email protected]",
"mobile": "+15555551003"
}
]
Are these really my only options? Is it not possible to use the new query API to query deeper into nested info?
Upvotes: 3
Views: 4283
Reputation: 600126
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('[email protected]').on('value', function(snapshot) {
console.log(snapshot.val());
});
See this jsbin for a working sample: http://jsbin.com/yikecu/1/edit?js,console
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" : ["[email protected]", '[email protected]'],
"mobile" : "+15555551001"
},
"simplelogin:002" : {
"emails" : ["[email protected]", '[email protected]'],
"mobile" : "+15555551001"
},
"simplelogin:003" : {
"emails" : ["[email protected]", '[email protected]'],
"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:
[email protected]: simplelogin:0001
[email protected]: simplelogin:0001
[email protected]: simplelogin:0002
[email protected]: simplelogin:0002
[email protected]: simplelogin:0003
[email protected]: 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.
Upvotes: 3