Find if child inside child data is exist or not Android Firebase

前端 未结 4 787
遥遥无期
遥遥无期 2020-12-12 08:08

This may be duplicate Question but i have tries alot but i didnt get my result, Here is my firebase structure

Updated

I need to fin

相关标签:
4条回答
  • 2020-12-12 08:11

    You may use a code like this with orderByChild() and equalTo() to compare all the fields with expensesName to a particular value say "oil" and know if such field exists or not.

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Expenses Details").child(uniqueID).child("Expense_Month").child("November_2018");
    
    ref.orderByChild("expensesName").equalTo("oil").addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    
                                if(dataSnapshot.exists()){
                                  // do what you want here
    
                                }
    
                            }
    
                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) { // ToDo: do something about exceptions too
    
                            }
                      )};
    

    Here uniqueID is the string that I can see in your database image, between the concerned nodes.

    Edit:

    After staring the structure for a minute, I think I have found the problem. This is not working because you have nodes inside November_2018 namely 3, 4 and so on, above the uniqueID node which is above expensesName.

    This hierarchy is what's causing the problem, I'd advise you to save count inside the uniqeID and not above it, this will make orderByChild() work.

    As usual with NoSQL databases: if you can't perform the use-case you want with your current data structure, you can typically change/expand your data structure to allow the use-case.

    And usually this is done by very directly mapping what you want to show on your screen to the structure in the database.

    Your database with dates can look like this:

    -UniqueId
    |
     -- expensesName
     -- date
     -- total
    
    0 讨论(0)
  • 2020-12-12 08:23

    To solve this, I recommend you to change your database structure a little bit. Because there is no way to use in a query dynamic values, or wildcards, you database schema should look like this:

    Expense_monts
       |
       --- November_2018
              |
              --- -LQ ... Gb
              |     |
              |     --- expensesName: "gross"
              |     |
              |     --- total: 66
              |     |
              |     --- day: "Monday"
              |
              --- -LQ ... LO
                    |
                    --- expensesName: "oil"
                    |
                    --- total: 33
                    |
                    --- day: "Tuesday"
    

    As you can see, there is no need to use that extra child (3, 4 and so on). In this case, a query that looks like this:

    expensesaddref = databaseReference.child(username).child("Expense_Month").child(monthyr);
    expensesaddref.orderByChild("expensesName").equalTo("oil")
        .addListenerForSingleValueEvent(/* ... */);
    

    Will work perfectly fine.

    0 讨论(0)
  • 2020-12-12 08:31

    As AlexMamo said you need to restructure database, i.e to flatten two levels hierarchy to only one level. As shown below:

    Expense_Month
       |
       --- November_2018
              |
              --- 3_-LQ ... Gb
              |     |
              |     --- expensesName: "gross"
              |     |
              |     --- total: 66
              |     |
              |     --- day: "Monday"
              |     |
              |     --- day: 3
              |
              --- 4_-LQ ... LO
                    |
                    --- expensesName: "oil"
                    |
                    --- total: 33
                    |
                    --- day: "Tuesday"
                    |
                    --- day: 4
    

    Notice each node in November_2018 is organised by the key formed by day followed by push Id separated by underscore. i.e "day_pushId". You can also add additional field for day (incase for particular day query is required). Then the query can be formed in the similar fashion as for "expensesName":

    //Here monthyr is the November_2018
              expensesaddref = databaseReference.child(username).child("Expense_Month").child(monthyr);
    expensesaddref.orderByChild("expensesName").equalTo("oil").addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    
                    if(dataSnapshot.getChildrenCount() > 0){
                        Toast.makeText(getContext(), "Exist", Toast.LENGTH_SHORT).show();
    
                    }else
                        Toast.makeText(getContext(), "not", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
    
                }
            });
    
    0 讨论(0)
  • 2020-12-12 08:36

    Thank you for all your answer Alex,Aadil,Pradyuman .As you all suggested i have restructure my query as Aadil mentioned and i can query all my data as required .This is my structure

    Expense_Month
       |
       --- November_2018
              |
              --- uniqueKey
              |     |
              |     --- expensesName: "gross"
              |     |
              |     --- total: 66
              |     |
              |     --- date_exp: "3_gross"
              |     |
              |     --- current_date: 3
    
              |
              --- uniquekey
                    |
                    --- expensesName: "oil"
                    |
                    --- total: 33
                    |
                    --- date_exp: "4_oil"
                    |
                    --- curent_date: 4
    
    0 讨论(0)
提交回复
热议问题