简体   繁体   English

检索 1 周范围内的 Firebase 数据

[英]Retrieve firebase data within a range of 1 week

I want to retrieve Firebase data within a range of say 1 week.我想在 1 周的范围内检索 Firebase 数据。 I can query and get data for a day like today, but how about for a range of say 1 week?我可以查询和获取像今天这样的一天的数据,但是例如 1 周的范围呢? This is the code that am currenly using for retrieving data for a given day这是当前用于检索给定日期的数据的代码

String mDate = DateFormat.getDateInstance().format(new Date());
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("expenses").child(onlineUserId);
    Query query = reference.orderByChild("date").equalTo(mDate);
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            myDataList.clear();
            for (DataSnapshot snapshot :dataSnapshot.getChildren()){
                Data data = snapshot.getValue(Data.class);
                myDataList.add(data);

            }
            todayItemsAdapter.notifyDataSetChanged(); 
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

I cannot find a way of retrieving data for a given range.我找不到检索给定范围的数据的方法。 Someone please help.有人请帮忙。 在此处输入图片说明

The Realtime Database is not designed for complex SQL-like queries.实时数据库不是为复杂的类似 SQL 的查询而设计的。

Not sure if your question refers to grouping results by week.不确定您的问题是否指按周对结果进行分组。 If all you need is a set of results that start and on a certain date and end on another date, you can use startAt(...) and endAt(...) like described in this answer or the spec .如果您只需要一组在某个日期开始并在另一个日期结束的结果,您可以使用startAt(...)endAt(...)本答案规范中所述

If you need anything more complex than that, you need to如果你需要比这更复杂的东西,你需要

  1. either take all results and filter them in your front end/app code要么获取所有结果并在您的前端/应用程序代码中过滤它们

  2. or use a Cloud Function to do the filtering on the server and passing the results to the front end.或者使用Cloud Function在服务器上进行过滤并将结果传递给前端。

  3. (Not recommended) You can record the week number (ie 45_2020) as a separate field in the document, and filter by that. (不推荐)您可以将周数(即 45_2020)记录为文档中的单独字段,并以此进行过滤。 It's messy and you would have to trust that the front end enters correct info in the field.这很混乱,您必须相信前端在现场输入正确的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM