I want to get the parent key value where the the "drinkManufacturerID" is equal to the currentUsers UID. However, I will not know the value of the VenueID or the PushID (see below). How can I check if the value for "drinkManufacturerID" is equal, without knowing these values. Is it possible without restructuring my database?
Relevant Code:
public class DealRawDataActivity extends AppCompatActivity {
DatabaseReference databaseDrinks;
DatabaseReference databaseManufacturer;
String keys;
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deal_raw_data);
databaseDrinks = FirebaseDatabase.getInstance().getReference("drinks").child("-LWLuM2nesg0uaP0dLSn"); //However, this string will not be know! How to get this string value?
//To later be used in listView
databaseDrinks.orderByChild("drinkManufacturerID").equalTo(currentFirebaseUser.getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot datas : dataSnapshot.getChildren()) {
keys = datas.getKey();
}
Toast.makeText(getApplicationContext(), keys, Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
//databaseManufacturer = FirebaseDatabase.getInstance().getReference("drinks").child(keys);
}
}
Database Structure:

I will not know the value of the VenueID or the PushID (see below)
You need to know the value of VenueID in order to be able to query your database at least for your the -LWLuM2nesg0uaP0dLSn node. So the following query:
.orderByChild("drinkManufacturerID").equalTo(currentFirebaseUser.getUid())
Will only get you all the drinks that exist within that particular object.
Is it possible without restructuring my database?
No, unfortunately there is no way in Firebase to query two or more Firebase nodes, two levels deep in the tree, as you intend to. To sovle this, you should duplicate your data. This practice is called denormalization and is a common practice when it comes to Firebase. If you are new to NoQSL databases, I recommend you see this video, Denormalization is normal with the Firebase Database for a better understanding.
Also, when you are duplicating data, there is one thing that need to keep in mind. In the same way you are adding data, you need to maintain it. With other words, if you want to update/detele an item, you need to do it in every place that it exists.
That being said, in your particular case, you should consider augmenting your data structure to allow a reverse lookup by creating another node named vanues, where you should add as objects all corresponding drink objects. So you database structure should look similar to this:
Firebase-root
|
--- vanues
|
--- -LZB-86uakRrWpI6VYzd (drink object)
|
--- drinkManufacturerID: "D1eY ... wrT5"
|
--- VenueID: "-LWLuM2nesg0uaP0dLSn"
Using this schema, you can simply query the database to get all drinks, where drinkManufacturerID equal to uid, using the following lines of code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference vanuesRef = rootRef.child("vanues");
Query query = vanuesRef.orderByChild("drinkManufacturerID").equalTo(uid);
query.addListenerForSingleValueEvent(/* ... */);
If you want to get all drinks from a specific vanue, simply use:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference vanuesRef = rootRef.child("vanues");
Query query = vanuesRef.orderByChild("VenueID").equalTo(VenueID);
query.addListenerForSingleValueEvent(/* ... */);
In which VenueID might hold a value like -LWLuM2nesg0uaP0dLSn.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With