Firebase - How do I write multiple orderByChild for extracting data?

firebase get data by key android
firebase orderbychild
how to retrieve data from firebase in android studio
android firebase query multiple orderbychild
firebase orderbychild reverse
firebase equalto multiple values
firebase get value of child
firebase get child value javascript

I'm looking to get data where two fields equal what I'm passing in.

Here's an example of my code:

this.refApp
  .orderByChild('userUid')
  .startAt(uid).endAt(uid)
  .orderByChild('jobId')
  .startAt(jobId).endAt(jobId)
  .on('value', (snap) => {
     //This currently doesn't get returned.
  });   

In the above example I don't get any compiler errors and code seems fine. However, I hard coded the data so that it would return an object where uid and jobid are equal to.

I can get this to work for one orderByChild but when I do two like above it doesn't seem to do anything.

You can only use one ordering method.

To query more, you'll need to rethink your data structure. Your current structure probably looks something like this:

{
  "key": {
     "id_1": {
        "userUid": "user_1",
        "jobId": "job_1"
     },
     "id_2": {
        "userUid": "user_1",
        "jobId": "job_2"
     },
     "id_3": {
        "userUid": "user_2",
        "jobId": "job_3"
     }
  }
}

With this structure you're limited to index off of one child key.

Now consider this structure:

{
   "key": {
      "user_1": {
         "id_1": {
            "jobId": "job_1",
            "userUid": "user_1"
         },
         "id_2": {
            "jobId": "job_2",
            "userUid": "user_1"
         }
      }
      "user_2": {
         "id_3": {
            "jobId": "job_3",
            "userUid": "user_2"
         }
      }
   }
}

This structure explicitly creates an index on uid. So now if you want to get all the jobs by user you can write this query:

var ref = new Firebase('<my-firebase-app>');
var uid = 'user_1';
var userRef = ref.child('key').child(uid);
var query = userRef.orderByChild('jobId');
query.on('value', (snap) => console.log(snap.val());

Work with Lists of Data on the Web, Firebase - How do I write multiple orderByChild for extracting data? I'm looking to get data where 2 fields equal what I'm passing in. Here's an example of my  When using orderByChild(), data that contains the specified child key is ordered as follows: Children with a null value for the specified child key come first. Children with a value of false for the specified child key come next. If multiple children have a value of false, they are sorted lexicographically by key.

An other way to do kind of multiple orderByChild in Firebase is to create an orderKey.

Imagine you have this in your base:

{
    userId: {
        firstName: 'Snow',
        lastName: 'Jon',
        birthYear: 283
    }
}

And you want to query: userRef.orderByChild('firstName').equalTo('Jon').orderByChild('lastName').equalTo('Snow')

You need to create a key on your user like this:

{
    userId: {
        firstName: 'Snow',
        lastName: 'Jon',
        birthYear: 283,
        orderName: 'JonSnow'
    }
}

So you can query like this: userRef.orderByChild('orderName').equalTo('JonSnow')

This also works with startAt and endAt if you want to query all the Snow born in a range of years. You first create the key:

{
    userId: {
        firstName: 'Snow',
        lastName: 'Jon',
        birthYear: 283,
        orderNameYear: 'Snow283'
    }
}

So you can query: userRef.orderByChild('orderNameYear').startAt('Snow280').endAt('Snow285')

This will return all Snow born between 280 and 285.

Retrieving Data, To read or write data from the database, you need an instance of You can use the Realtime Database Query class to retrieve data sorted Calling an order-by method multiple times in the same query throws an error. The call to the orderByChild() method specifies the child key to order the results by. Order and limit data with Cloud Firestore Cloud Firestore provides powerful query functionality for specifying which documents you want to retrieve from a collection. These queries can also be used with either get() or addSnapshotListener() , as described in Get Data .

It's not supported.

Depending on the kind of query you need to run on an entity, you will either have to store you jobs by user like @david-east suggested or add a property combining the two properties:

{
 jobId: 'J123',
 userId: 'provider:123',
 jobIdUserIdIndex: 'J123/provider:12345',
 [...]
}

But usually, there are no way to allow all the queries you need. You either have to filter objects on the client side, or duplicate the data to created different view of the data; you will have the job data saved at /jobs/$jobId, /users/$userId/jobs/$jobId and maybe /pendingJobs/$jobId.

I find it's easier to use the orderBy* method only for ordering entities and to duplicate the data to select subsets of them.

I wish Firebase would support indexes combining multiple properties, but for now you need to build those yourself. However having different views allows to apply different security rules to them: e.g. Only one user should have access to /users/$userId/jobs/, only the admin should have access to /jobs/$jobId.

Firebase: Now with more querying!, Notice that we used the value event type in the example above, which If you want to retrieve only the data on each new post added to your  Firebase allows you to do ad-hoc queries on your data using an arbitrary child key. If you know in advance what your indexes will be, you can define them via the .indexOn rule in your Firebase Realtime Database Rules to improve query performance. Defining Data Indexes. Firebase provides powerful tools for ordering and querying your data

How To Query, Filter And Sort Firebase Real-Time Database , With our new querying methods you can order and retrieve your data our orderBy functions: orderByChild() , orderByKey() or orderByPriority() . firebase of dinosaur facts to demonstrate how you can write complex, We can also use our limit methods to return the two heaviest dinosaurs in our Firebase: The REST API accepts several query parameters when reading data from our Firebase database. Listed below are the most commonly used parameters. For a full list, refer to the REST API Reference. auth. The auth request parameter allows access to data protected by Firebase Realtime Database Rules, and is

Collection Queries with Firebase, Learn how to query the Firebase Realtime Database using events. Enable Read and Write Permission to the Firebase Database Instead of using callback functions, you can use promises for better multiple Using orderByChild(), you can sort the data by any child (think about the child key as a column name in SQL​). Firebase - Queries - Firebase offers various ways of ordering data. In this chapter, we will show simple query examples. We will use the same data from our previous chapters.

How to use Firebase Queries In Flutter, Make sure you know how to connect to Firebase and run basic read/write operations before continuing. orderByKey; orderByChild; orderByPriority; orderByValue You'll want to use limits for most of your queries to avoid pulling down any more data than necessary. You've got two options for limits:. In this video, I demonstrate how to save data to the cloud using the database service Firebase. This video is part of the "Programming from A to Z" ITP class. Next video: https://youtu.be

Comments
  • See stackoverflow.com/questions/33336697/…, stackoverflow.com/questions/27432030/…, stackoverflow.com/questions/32538312/…
  • Another simple scenario I'm stuck with is for example I want to list my contacts by last_name then by first_name. How do you do that in Firebase?
  • Hi David, I see what you mean about the data structure. Is there really no other way to get an object where two fields are equal to?
  • I'm just looking to do a simple where statement with an And like SQL.
  • That does not exist in Firebase land at the moment. You can do something like a SELECT * FROM table WHERE userid = "user_1", but no AND statement. Structuring your data like I specified allows you to do some like the AND. Make sense?
  • Yeah it makes sense, but its not going to work for what I need. Do you know when "And" will be available or something similar in Firebase?
  • Firebase is NoSQL so it's not going to have to the same feature set as a SQL database. What about the solution is not going to work?
  • Can you help me to load more data with this type of query onSwipeRefresh?