从特定日期查询Firebase数据库

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

Query from firebase database from specific date

问题

我想查询特定日期的数据,数据的格式如下。

databaseReference = database.getReference("TopScoreUpdate");
databaseReference.orderByChild("date/date").equalTo(29).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        Log.i("entityCheck", String.valueOf(dataSnapshot.getChildrenCount()));
        for (DataSnapshot snapshot1:dataSnapshot.getChildren()) {}

对于上面的代码,我没有得到结果,它显示没有找到结果。我在Firebase规则的“.Index”中声明了。

英文:

I want to query for data of a specific date the formatting of data is specified below.

[![][1]][1]
databaseReference = database.getReference("TopScoreUpdate");  
 databaseReference.orderByChild("date/date").equalTo(29).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Log.i("entityCheck", String.valueOf(dataSnapshot.getChildrenCount()));
                for (DataSnapshot snapshot1:dataSnapshot.getChildren()) {}

for the above code written I am not getting result it shows no result found. I have declared in ".Index" of Firebase rule.

答案1

得分: 0

以下是翻译好的内容:

这是因为你缺少一个子项。为了解决这个问题,请使用以下代码行:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference topScoreUpdateRef = rootRef.child("TopScoreUpdate");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            long date = ds.child("prful/date/date").getValue(Long.class);
            Log.d("TAG", "date: " + date);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //不要忽视错误!
    }
};
topScoreUpdateRef.orderByChild("prful/date/date").equalTo(29).addListenerForSingleValueEvent(valueEventListener);

请注意,在你的引用中需要添加对 prful 的显式调用。日志中的结果将会是:

date: 29

在日志中会打印出与它在你的数据库中出现的次数相同的次数。

英文:

> for the above code written I am not getting result it shows no result found.

This is happening because you are missing a child. To solve this, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference topScoreUpdateRef = rootRef.child("TopScoreUpdate");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            long date = ds.child("prful/date/date").getValue(Long.class);
            Log.d("TAG", "date: " + date);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
    }
};
topScoreUpdateRef.orderByChild("prful/date/date").equalTo(29).addListenerForSingleValueEvent(valueEventListener);

See, the explicit call to prful should be added in your reference. The result in the logcat will be:

date: 29

Printed out as many times as it appears in your database.

huangapple
  • 本文由 发表于 2020年7月30日 14:57:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63167785.html
匿名

发表评论

匿名网友

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

确定