LaVOZs

The World’s Largest Online Community for Developers

'; javascript - Firebase Database query equalTo - LavOzs.Com

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?

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.

Related
How can I get query string values in JavaScript?
Query based on multiple where clauses in Firebase
Querying at firebase not working with orderByChild/equalTo with a string child field
Firebase database delete all nodes equalTo a particular value
Simple Android / Firebase orderByChild / equalTo
Returning a single child's value on Firebase query using orderByChild and equalTo
How to add child to a List in Firebase Realtime Database with a cloud function?
How to get child Snapshots inside snapshots on Google Cloud functions (Firebase)?
How to call a function when firebase query doesn't match anything in the database