1

I am wanting to fetch all users that contain a child node 'notificationToken' and exclude all users that don't have this node. I am using the following:

const ref = admin.database().ref('/Users');
const users = await ref.orderByChild('notificationToken').equalTo(****WHAT GOES HERE****).once('value').then((snapshot) => snapshot.val());

I'm not sure if equalTo can do what I want, I know I can use it to match exact strings or booleans, but how can I use it to match if the user has the notificationToken node?

1 Answer 1

1

You can use equalTo to match the exact value of a node/property in the database.

So:

ref.orderByChild('notificationToken').equalTo(value)

Returns child nodes of ref for which the notificationToken property has the exact same value as value. From that last link it follows that you can use startAt(null) to get all child nodes that have the property, no matter what its value or type.

The value can be any allowed JSON type, and must match the type that is stored in the database. So if notificationToken is a string value, value should be a string too.

For more on this see the Firebase documentation on sorting and filtering data, and specifically the section on how query data is ordered.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi thanks for the reply. I know I can match a value, but I am wanting to select the data if it has the node regardless of the value inside. I am using startAt now as I know what the value will always start with, but was hoping for a more robust way...
This is why I explicitly called out the how data is sorted documentation. You can startAt(null) to get all child nodes that have the property.
thanks for the reply. I have tried using startAt(null) but it returns all objects not just the ones that have a notificationToken node.
Hmmm.... I thought null was the one. But it turns out startAt(false)` may be the right approach. See my repro here: jsbin.com/zowafug/3/edit?js,console
Hi thanks again. Have tried startAt(' ') & startAt(false) and neither gets the result. Just want to confirm that I need to check for the existence of 'notificationToken' not it's value. i.e {name: 'Test', notificationToken: 'sdsd' }, {name: 'Test 2'} - I want to get the object with name Test
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.