I need to update the data in Firebase database. I have displayed the data in Recyclerview. I need to get the child reference of the position i click in Recyclerview. An not able to use getRef() there to get the reference.
You need to generate a unique key with
String unique = reference.push().getKey();
when saving data to firebase, then you can use that unique key to modify content of that node later.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Transactions").child(uniqueId)...
You can show your getRef(position) method in your question, maybe another solution can come from there.
Related
I want to be able to differentiate my content between type and get all other data but I don't know how to get past the IDs after content.
I want every data from -NBC22CHEk3ebNxdlYOi, -NBC-Nfz6e5yppFlxU7S, -NBC1TVY-TzT7yfrSA3o
and later be possible to get new video id's from existing and new users
/content/{user_uid}/{content_uid}
If I want only the content from one user then I can write this:
val uid = FirebaseAuth.getInstance().uid
// url is just a placeholder for the real database url
val ref = FirebaseDatabase.getInstance(url).getReference("/content/$uid")
I want all content with the type value "video" from all users.
That's actually not possible with your actual database structure. All queries in the Realtime Database work on a flat list of nodes, where the value on which you want to perform the filter/order on, must be at a fixed path under each direct child node.
Unfortunately, that's not your case, because you have a dynamic key under the content node, which is actually the UID, and which also means that you can't filter on type. If you want to allow that, then you have to create a flat list of videos, where the UID (SIH5...89j1) is a property inside each video object. This is already achieved since publisher_uid holds exactly that.
In short, remove the extra (UID) level from your database tree. Or, leave it as it is, and denormalize the data. Basically create a new structure like this:
/content/{content_uid}
See, there is no UID involved. If you're new to NoSQL databases, this might sound some kind of weird, but it's actually a quite common practice.
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() })
I'm trying to edit the name of the classroom shown in the image below without losing the children data, how to do that in android ?
There is no way for doing this. There is no API that allows you to change the name of a node in a Firebase database. What can you do instead, is to create another node with the desired name, containing the same childrens and then remove the old one.
Is there any way to select specific properties from firebase Realtime Database? I know there is a way to retrieve selected properties from firestore but how can get via Realtime database using Node.js
I want only Notes from everyone nothing else.
Suppose i just want to select Notes from Allergy here is my sample code which i tried but not successes...
admin.database().ref(`vitals/Allergy`).select('Notes').then(result => {//here is my result.....})
But it shows me that select is not a function.......
Realtime Database doesn't support "projections" like this (neither does Cloud Firestore). If you are going to query across multiple child nodes, you are going to get each entire child node that matches the query. Even if you want just one property of each child, you can't avoid the cost of downloading the entire child.
If your app is very sensitive to performance on these types of queries, consider duplicating the data such that there is another branch of your database that contains only the "Notes" property, and query that branch alone. This duplication is common in NoSQL type databases, and is call the "fan out" technique.
If you want to save download band then i can't help you further, otherelse:
let ArrayOfAllDownloadedNotes = [];
firebase.database().ref('yourRootFolders/vitals/Allergy').once((snapshot)=>{
ArrayOfAllDownloadedNotes = [...ArrayOfAllDownloadedNotes, snapshot.val().Notes];
});
//Reapeat the firebase formula for every child you want retrieve the Notes from
//You may also use forEach((item)=>{}) function for each folder you want to retrieve the //notes from if you want
console.log(My Array of Notes:',ArrayOfAllDownloadedNotes );
I am saving some data in FirebaseDatabase under a child as a key:value pair.
The problem is that data is getting saved as pushed, like if data1 is saved already than data2 will get saved below it. I want to save it above this already saved data.
Here is the data structure:
- branch
- child
- uniqueKey1: data1
- uniqueKey2: data2
I am saving the data using this code:
String key = mDatabase.child("branch").child("child").push().getKey();
mDatabase.child("branch").child("child").child(key).setValue(data);
What I want is the structure below:
- branch
- child
- uniqueKey2: data2
- uniqueKey1: data1
How to save the newly added data above the already saved data? Please let me know.
In your firebase database, childs in a node are ordered in alphabetical order, no matter which order you save them.
If you want to retrieve them later in some specific order, let's say order by date added, you might want to create a timestamp reference.
Check here:
How to sort value inside a Firebase child to the rest?
Edit: a lot of answers and edited questions while writing my answer, but as others mentioned you should not worry about the order you see the data in the database, you should only care to provide the right structure to retrieve the data correctly.
As I said, in the DB the childs are ordered in alphabetical order so if you insist on ordering it by date added you should figure out a way to update the key accordingly and then update the whole node.
I don't think the order in which you save data is important as long as you have a strategy to retrieve it correctly in your order of choice. Firebase provides API to retrieve data ordered by either key or value.
Work with Lists of Data on Android
If you just want to retrieve the record first which was added last, you can put a timestamp that accompanies your data. There are also methods that gets you last record of a collection.
What you are trying to do is update data1. Then what you have to do is,
Get reference of the node you want to update.
DatabaseReference ref = mDatabase.child("branch").child("child").child("data1");
then update the child node. I'm assuming there is a child name under data1, whose value is 'Foo'. And now you want to update it 'Bar'.
Hashmap<String, String> map = new Hashmap();
map.put("name","Bar");
ref.updateChildren(map);
This is how you update a node. If you want to completely replace the node, then you can delete the node and by calling removeValue() method and pushing the data again!.