Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query multiple values for a given key in firebase

I attempted to use an 'OR' || statement in the equalTo() method, but this does not seem to work. Do I have to make a separate call for each value or is there a way to make conditional queries in firebase?

export const startSetContent = () => {
  return (dispatch, getState) => {
    const ref = database.ref("content");
    return ref
      .orderByChild("category")
      .equalTo('one-act-play' || 'ten-min-play' || 'full-length-play')
      .once('value')
      .then(snapshot => {
        const content = [];
        snapshot.forEach(childSnapshot => {
          content.push({
            id: childSnapshot.key,
            ...childSnapshot.val()
          });
        });
        dispatch(setContent(content));
      });
  };
}; 
like image 281
Jordan Hensley Avatar asked Jan 12 '18 22:01

Jordan Hensley


People also ask

What is getKey () in Firebase?

public String getKey () Returns. The key name for the source location of this snapshot or null if this snapshot points to the database root.

Is Firebase key value?

Firebase is categorized as a NoSQL database program, which stores data in JSON-like documents. In Firebase, a document is a set of key-value pairs defined by a schema.

What is orderByChild in Firebase?

Firebase orderByChild once child_added is only giving one child. 0. Returning a single child's value on Firebase query using orderByChild and equalTo. 3. unspecified index when searching data with firebase cloud function on nested object running nested Query.

How do you filter data in Firebase realtime database?

We can filter data in one of three ways: by child key, by key, or by value. A query starts with one of these parameters, and then must be combined with one or more of the following parameters: startAt , endAt , limitToFirst , limitToLast , or equalTo .


2 Answers

What you typed there was a logical OR that JavaScript the programming language interprets. The result of the expression:

'one-act-play' || 'ten-min-play' || 'full-length-play'

is going to be simply:

'one-act-play'

So, Firebase Realtime Database will not see what you're trying to tell it.

Firebase Realtime Database has no concept of a kind of query that has ORs in it. You will have to query each type of thing separately and merge them together in your code as needed.

like image 95
Doug Stevenson Avatar answered Oct 22 '22 17:10

Doug Stevenson


There is no concept of doing that you are asking for. You can do a turn around in which you can get the values on basis of one filter one-act-play using equalTo and rest you can filter on user end like:

export const startSetContent = () => {
  return (dispatch, getState) => {
    const ref = database.ref("content");
    return ref
      .orderByChild("category")
      .equalTo('one-act-play')
      .once('value')
      .then(snapshot => {
        const content = [];
        Object.keys(snapshot.val()).map(k => {
        if(k == 'ten-min-play' || k == 'full-length-play'){
        snapshot.forEach(childSnapshot => {
          content.push({
            id: childSnapshot.key,
            ...childSnapshot.val()
          });
        });
        }
        dispatch(setContent(content));
      });
  };
}; 

Or you can use library made to fulfil this kind of requirement Querybase

like image 29
Himanshu Avatar answered Oct 22 '22 16:10

Himanshu