0

There are multiples dates and data for each date. Each date and data store under a unique key.

"Data":{
   "K2ngvpioRUYF4bRM07Da5cbAjE53":{
      "-M3jNjCuGdMCwt1Czpwz":{
         "Date":"2020-3-30",
         "Scale":"3"
      },
      "-M3jQWxm7z0EQYgkVenX":{
         "Date":"2020-4-29",
         "Scale":"4"
      },
      "-M5hxn-rCJICUvRcMZJu":{
         "Date":"2020-4-24",
         "Scale":"2"
      }
   }
}

I would like to calculate the total number of scales for monthly (The main point is monthly). Instead of using "startAt" and "endAt":

   Query query = ref.orderByChild("Date").startAt("2020-3-1").endAt("2020-3-31");
   Query query2 = ref.orderByChild("Date").startAt("2020-4-1").endAt("2020-4-30");

Is there any better way to loop for all month and year? I think the above code is hard if I have dates of different years and months.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Mouris
  • 67
  • 1
  • 8
  • first of all, consider add date in UTC format on database see this https://stackoverflow.com/questions/2818086/android-get-current-utc-time for UTC date then consider to move on to firestore which have more advance query option so maybe your problem solve – Burhan Khanzada May 01 '20 at 02:45
  • Hi, I am not sure about how UTC format will help in looping for all month and year – Mouris May 01 '20 at 16:14
  • This answer will help you out, https://stackoverflow.com/a/67669844/8168140 – gsm May 24 '21 at 09:43

1 Answers1

0

If your ref object is pointing to the database root, then your reference is not correct since you are missing the Data and the ID of the user from it. See the Data and the K2ngvpioRUYF4bRM07Da5cbAjE53 are present in your tree? Both children should be added to your query. Besides that, you are using for the Date property, String values which is actually also not correct. You should use a Timestamp, as explained in my answer from the following post:

That being said, a correct query should look like this:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
Query query = ref.child("Data").child(uid) //Added both children
    .orderByChild("Date")
    .startAt(startLongValue)
    .endAt(endLongValue);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hi, can I know what do the startLongValue and endLongValue mean? – Mouris May 01 '20 at 16:10
  • Those are the exact Long values, that you should pass to those methods. Here `.startAt("2020-3-1")` you are passing a String representation, you need to use a Long, as explained in my answer from this [post](https://stackoverflow.com/questions/43584244/how-to-save-the-current-date-time-when-i-add-new-value-to-firebase-realtime-data). Passing String values, will never work. – Alex Mamo May 01 '20 at 16:55
  • Hi, I have improved my question and would like to ask for your help. I will click the checkmark if your answer is helpful. Thank you for spending your time for help. – Mouris May 03 '20 at 06:31
  • I rolled back the question as it is different than the initial one. Please don't edit questions with other questions. If you encounter new problems, it's best to post a new question using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo May 03 '20 at 12:41
  • You need to store dates like "2022-05-08" OR "2020-12-01". add 0 before a month and date less than 10. This trick works for me. – Muzammal Hussain May 05 '22 at 21:14