admin管理员组

文章数量:1296469

This question is about implementing firebase deep querying. Consider the following structure in firebase:

Here, my ref is pointing to the root of the structure which is /messages. So I have :

var ref = new Firebase("");

I wish to query those message Id's having member = -752163252 . So basically the returned object should be the one with key 655974744 . How do I go about doing this in firebase?

Here's what I tried in my console:

ref.orderByChild("members").equalTo(235642888).on('value', function(snap){console.log("Found ",snap.val())});

Result was:

VM837:2 Found  null

I sense there is a missing link somewhere. Basically, I want to know if there is any way to query the deep nested data without having the parent key id's (in this case 25487894,655974744) .

Also, if I may extend my question, is there a way to add a listener that calls back when a new messageId (in this case 25487894,655974744) is added containing member = -752163252 .

Hope my question is clear enough. Any help is greatly appreciated!

EDIT:

I have already looked at the dinosaurs example, and that's how I tried what I tried but it didn't work.

This question is about implementing firebase deep querying. Consider the following structure in firebase:

Here, my ref is pointing to the root of the structure which is /messages. So I have :

var ref = new Firebase("https://cofounder.firebaseio./messages");

I wish to query those message Id's having member = -752163252 . So basically the returned object should be the one with key 655974744 . How do I go about doing this in firebase?

Here's what I tried in my console:

ref.orderByChild("members").equalTo(235642888).on('value', function(snap){console.log("Found ",snap.val())});

Result was:

VM837:2 Found  null

I sense there is a missing link somewhere. Basically, I want to know if there is any way to query the deep nested data without having the parent key id's (in this case 25487894,655974744) .

Also, if I may extend my question, is there a way to add a listener that calls back when a new messageId (in this case 25487894,655974744) is added containing member = -752163252 .

Hope my question is clear enough. Any help is greatly appreciated!

EDIT:

I have already looked at the dinosaurs example, and that's how I tried what I tried but it didn't work.

Share edited Feb 24, 2016 at 19:53 Frank van Puffelen 600k85 gold badges889 silver badges859 bronze badges asked Feb 24, 2016 at 19:25 Rohan DalviRohan Dalvi 1,2652 gold badges16 silver badges39 bronze badges 2
  • 1 You've included a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export button in your Firebase database. Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. – Frank van Puffelen Commented Feb 24, 2016 at 19:52
  • @FrankvanPuffelen Thanks for mentioning it. Sorry , I will keep that in mind. – Rohan Dalvi Commented Feb 24, 2016 at 20:06
Add a ment  | 

1 Answer 1

Reset to default 10

Your query asserts that the "members" node is an integer with value 235642888, which it is not, naturally. It's an object containing a list of keys.

Instead, you would want to use the deep child query syntax and something like the following:

ref.orderByChild("members/235642888").equalTo(235642888);

Note that you don't really need the value of the key to be the key itself. You could save storage by just setting this to a binary true or 1. The query would be much the same:

ref.orderByChild("members/235642888").equalTo(true);

本文标签: javascriptFirebase deep queryingStack Overflow