1

I have a node called quotes in Firebase. I'm facing issues while fetching data in Android for a particular range. I want to fetch 3 continues quotes id starting from 2. Here is my query database:

"quotes" : {
"-L75elQJaD3EYPsd4oWS" : {
  "authorName" : "Hellen v",
  "famousQuote" : "When one door of happiness closes, another opens; but often we look so long at the closed door that we do not see the one which has been opened for us.",
  "id" : "1",
  "uploadedBy" : "Admin"
},
"-L7GOvDNI-o_H8RvNwoN" : {
  "authorName" : "Rocky Balboa",
  "famousQuote" : "It's not about how hard you can hit; it's about how hard you can get hit and keep moving forward.",
  "id" : "2",
  "uploadedBy" : "Admin"
},
"-L7GP9oBv5NR1T6HlDd4" : {
  "authorName" : "African proverb",
  "famousQuote" : "If you want to go fast, go alone. If you want to go far, go together.",
  "id" : "3",
  "uploadedBy" : "Admin"
},
"-L7GPjM1F3_7Orcz0Q1q" : {
  "authorName" : "A.P.J Abdul Kalam",
  "famousQuote" : "Don’t take rest after your first victory because if you fail in second, more lips are waiting to say that your first victory was just luck.",
  "id" : "4",
  "uploadedBy" : "Admin"
},

Below is the rule which I'm using for quotes

"quotes": {
 ".indexOn": ".value"
}

How can I get quotes which has id 2,3 and 4?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Can you explain what have you tried and what went wrong? What issues are you facing? – Yosi Pramajaya Sep 29 '18 at 10:53
  • I've tried below queries **1**. `Query quotes = mFirebaseDatabase.child("quotes").orderByValue .startAt(2);` - Here I am getting all nodes which is present inside quotes. **2** `Query quotes = mFirebaseDatabase.child("quotes").orderByValue .startAt(2).limitToFirst(3);` - Here I am getting top 3 results. – Neeraj vishwakarma Sep 29 '18 at 11:03
  • And what went wrong? did you get it until 4? Or no? – Yosi Pramajaya Sep 29 '18 at 11:17

1 Answers1

2

If you have more than 4 records in your database, to solve this, you can use a query in which you should combine startAt() and endAt() methods to limit both ends of your query like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("quotes").orderByChild("id").startAt("2").endAt("4");
query.addListenerForSingleValueEvent(/* ... */);

Please see here more informations about Firebase Query's startAt() method:

Create a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.

And here are more informations about Firebase Query's endAt() method:

Create a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.

Edit: According to your comment, if you only want the items that have the id property set to 2, 3 and 4, you should use nested queries like this:

Query queryTwo = rootRef.child("quotes").orderByChild("id").equalsTo("2");
queryTwo.addListenerForSingleValueEvent(
    List<Item> list = new ArrayList();
    list.add(itemTwo);
    Query queryThree = rootRef.child("quotes").orderByChild("id").equalsTo("3");
    queryThree.addListenerForSingleValueEvent(
        list.add(itemThree);
        Query queryFour = rootRef.child("quotes").orderByChild("id").equalsTo("4");
        queryFour.addListenerForSingleValueEvent(
            list.add(itemFour);

            //Do what you need to do with the list that contains three items
        );
    );
);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • The `id` values are stored as strings, so `.startAt("2").endAt("4")`. – Frank van Puffelen Sep 29 '18 at 14:30
  • @Alex, I have more than 500 records in my database. When I'm using `Query query = rootRef.child("quotes").orderByChild("id").startAt("2").endAt("4");` I am getting the records which has the id as 2,3 and 4 but along with also getting the records which has id as 21,22,40,42.. etc. I need only 2,3 and 4. – Neeraj vishwakarma Sep 29 '18 at 14:55
  • 1
    @FrankvanPuffelen Oh my bad, you're right Puf, just updated my answer. If you see in the future any other inconsistency in my code, please feel free to correct it. Thanks! – Alex Mamo Sep 29 '18 at 15:13
  • If you consider moving to Cloud Firestore, you can achieve this in a more simpler way, as explained in my answer from this **[post](https://stackoverflow.com/questions/50118345/firestore-merging-two-queries-locally/50132229)**. – Alex Mamo Sep 29 '18 at 15:22
  • Is there everything alright, can I help you with other informations? – Alex Mamo Sep 30 '18 at 10:43