获取 Firebase 数据在1周范围内

huangapple go评论59阅读模式
英文:

Retrieve firebase data within a range of 1 week

问题

以下是您提供的代码的翻译部分:

我想在一周的范围内检索Firebase数据。我可以查询并获取今天这一天的数据,但是如何查询一周的数据呢?以下是我目前用于检索给定日期数据的代码示例:

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 want to retrieve Firebase data within a range of say 1 week. I can query and get data for a day like today, but how about for a range of say 1 week? 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.
获取 Firebase 数据在1周范围内

答案1

得分: 2

实时数据库并非为复杂的类SQL查询而设计。

不确定您的问题是否涉及按周分组的结果。如果您只需要一组从特定日期开始并在另一个日期结束的结果,您可以使用类似于此答案规范中所述的startAt(...)endAt(...)

如果您需要比那更复杂的操作,您需要:

  1. 要么获取所有结果并在前端/应用程序代码中对其进行筛选。

  2. 要么使用云函数在服务器上进行筛选,并将结果传递给前端。

  3. (不推荐)您可以将周数(例如45_2020)作为文档中的单独字段记录下来,并通过该字段进行筛选。这会比较混乱,并且您必须相信前端会在字段中输入正确的信息。

英文:

The Realtime Database is not designed for complex SQL-like queries.

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.

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.

  3. (Not recommended) You can record the week number (i.e. 45_2020) as a separate field in the document, and filter by that. It's messy and you would have to trust that the front end enters correct info in the field.

huangapple
  • 本文由 发表于 2020年9月30日 18:24:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64135575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定