How to get retrieve items from the following realtime database structure? - android

database structure
I'm completely new to realtime database and nosql and want some help.
How do I fetch all the data. from only the keys starting with 2020, from the following database structure?

How do I fetch all the data. from only the keys starting with 2020, from the following database structure?
Firebase doesn't provide a way to filter the records by keys that start with a particular String. The most simple solution I can think of is to change the "year" property in your database to be of type number and not String, otherwise, you'll have a lexicographical order. To solve this, you need to use a query that looks like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference calendarRef = rootRef.child("calendar");
Query yearQuery = calendarRef.orderByChild("year").startAt(2000).endAt(2001);
yearQuery.addListenerForSingleValueEvent(/* ... */);
This will return all records where the "year" property holds the value of 2000.

Related

How to get just one child from FIrebase Realtime Database instead of whole list

I am implementing Firebase Realtime Database for my app.
The database contains a lot of entries that will continue to grow in the future.
I want to retrieve only a specific child from the database, but i can't find any implementation for doing that.
In Firebase too the implementation retrieves all the entries from the database.
This will be cumbersome in the future.
Is there any way to retrieve only specific entries or just one entry(depending upon the customisation)?
Please help.
#ALex Mamo: This is the database image
Perhaps you are refering to shallow query, which only read the first child ? In this case you might want to look at the API implementation as mentioned here:
Firebase (2016) Shallow Query
There are two different ways of getting it. You'll have to change 'messages' to the name of your database. Replace PostTitle with the specific data you're trying to receive.
var rootRef = firebase.database().ref().child("messages");
rootRef.on("child_added", snap=> {
var postTitle = snap.child("PostTitle").val();
or
exports.sendPushNotification = functions.database.ref('/messages/{id}').onWrite((change, context) => {
const afterData = change.after.val();
const postTitle = afterData.PostTitle;
if you want retrieve few records this might help. as per image structure attached.
you can restructure the data, and use set method with keys as 0,1,2.... instead of "LK34h...."->0 .
use set method and store latest index value.
you retrive like this
firebase.database().ref().child('users_Post/0').on('value',sn=>{ console.log(sn.val())//use sn.val() })

How do I get the key of this level?

Hi,
I have this problem in query in Firebase. The orderbykey() and orderbychild() doesn't work. How do I query this one?
cheers, thanks in advance.
Code Snippet:
Query mRef = mRootRef.child("Messages").orderByKey().equalTo(mAuth.getCurrentUser().getUid());
Query mRef = mRootRef.child("Messages").orderByChild("commWith").equalTo(mAuth.getCurrentUser().getUid());
i have just re design my JSON at firebase like in the image below, so that i could simply get the unique keys per data

How apply sql query to Firebase in Xamarin Android?

I need get return data from Firebase. In Sql query it would be like this:
Select * from * Group By field1, field2 HAVING count(*)=1
How to apply above to FireBase in Xamarin Android. So I have node "chats", here I want retrieve record only not repeated on field "user_email":
chats
|-...
|---date_and_time:
|---user_email:
|-...
|---date_and_time:
|---user_email:
...
at this moment I get whole data from "chat", here code:
public List<MessageContent> listOfContacts = new List<MessageContent>();
FirebaseClient firebase;
...
firebase = new FirebaseClient(GetString(Resource.String.database_name));
...
var firebaseContacts = await firebase.Child("chats")
.OnceAsync<MessageContent>();
foreach (var item in firebaseContacts)
listOfContacts.Add(item.Object);
Firebase's realtime database is a NoSQL database. Trying to directly map a SQL query to it is going to give you a rough time. I highly recommend reading NoSQL data modeling and watching Firebase for SQL developers.
In NoSQL databases you typically get more complex write operations and in return get more performant/scalable read operations. So in your case, that could mean that you actually store a list of the records that you're trying to query and update that list with every write.
Some previous questions that deal with similar scenarios can be found in this list.

Query nested data from Firebase Real-time database Android

I have the structure in the image below and I want to get the whole data from the node "aaa1234" querying by "id_Usuario". How can I do that query?
I tried:
DatabaseReference root = database.getReference().child("Eventos").child("participantes");
Query query = root.orderByChild("id_Usuario").equalTo(2);
Just to be more clear, the "aaa1234" is a custom key, so it will be different in the next node.
I see two mistakes.
The first one is a typo, which I already marked in my comment. You're storing the user ID as a number, so shouldn't have quotes around the value in equalTo. So: .equalTo(2)
The second mistake is in the way you try to query:
DatabaseReference root = database.getReference().child("Eventos").child("participantes");
This will create a reference to the non-existing child Eventos/participantes. What you want to do instead is query Eventos, but then against property participantes/id_Usuario. So
DatabaseReference root = database.getReference().child("Eventos");
Query query = root.orderByChild("participantes/id_Usuario").equalTo(2);

Android Firebase Database query select with condition

I want to SELECT * FROM products WHERE 'birthday' = true;
But can I do the same with Firebase Database ?
I have tried
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref. child("products").orderByChild("birthday").equalTo(true).addChildEventListener ........
Firebase Database image
But it doesn't work.Please help.
f my data structure is bad, please also advise how to create a better structure.
The screenshot of the database shows that birthday is not a direct child of the product node: there's an extra step, for_purpose.
Your query must follow the schema precisely, therefore update your query code like this:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.child("products")
.orderByChild("for_purpose/birthday")
.equalTo(true)
.addChildEventListener ...
Note that for this to work effectively, you must set up indexes for every possible for_purpose child in your security rules. Or at least for those that you use in queries. The indexes must match your query exactly, putting an index on for_purpose is not enough.
For this question, you need at least the following:
{
"rules": {
"products": {
".indexOn": [
"for_purpose/birthday",
"for_purpose/anniversary",
"for_gender/male",
...
]
}
}
}

Categories

Resources