I am saving data as follows in the Firebase:
I want to find all records that have #Yahoo in their title. What will be the exact query for that?
I am confused with the random key created. I am not sure how to handle this so i am posting it here.
Firebase firebase = new Firebase(Constants.FIREBASE_URL_USER_TASKS).child(Utils.encodeEmail(unProcessedEmail));
Query queryRef = firebase.orderByKey().startAt("#" + mHashTag).endAt("#" + mHashTag + "\uf8ff");
queryRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mTasksList.clear();
mAdapter.notifyDataSetChanged();
for (DataSnapshot task : dataSnapshot.getChildren()) {
mTasksList.add(task.getValue(TaskModel.class));
}
mAdapter.notifyItemRangeInserted(0, mTasksList.size());
mSwipeToRefresh.post(new Runnable() {
@Override
public void run() {
mSwipeToRefresh.setRefreshing(false);
}
});
}
@Override
public void onCancelled(FirebaseError firebaseError) {
mSwipeToRefresh.post(new Runnable() {
@Override
public void run() {
mSwipeToRefresh.setRefreshing(false);
}
});
}
});
Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.
We can filter data in one of three ways: by child key, by key, or by value. A query starts with one of these parameters, and then must be combined with one or more of the following parameters: startAt , endAt , limitToFirst , limitToLast , or equalTo .
After creating a new project, navigate to the Tools option on the top bar. Inside that click on Firebase. After clicking on Firebase, you can get to see the right column mentioned below in the screenshot. Inside that column Navigate to Firebase Realtime Database.
You cannot search for any item whose title contains #Yahoo
. See:
How to perform sql "LIKE" operation on firebase?
You can however search items whose title begins with #Yahoo
:
Firebase firebase = new Firebase(Constants.FIREBASE_URL_USER_TASKS).child(Utils.encodeEmail(unProcessedEmail));
Query queryRef = firebase.orderByChild("title").startAt("#" + mHashTag)
To make this work well, you have to add an index to your Firebase rules:
"rules": {
"userTasks": {
"$email": {
".indexOn": "title" // index tasks in their title property
}
}
}
This is working with the Google Firebase.
DatabaseReference mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
Query query = mFirebaseDatabaseReference.child("userTasks").orderByChild("title").equalTo("#Yahoo");
query.addValueEventListener(valueEventListener);
ValueEventListener valueEventListener = new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
{
//TODO get the data here
}
}
@Override
public void onCancelled(DatabaseError databaseError)
{
}
};
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