Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to rename a key in the Firebase Realtime Database?

Tags:

I was wondering, is there a way to update the key value?

Let´s use the following data:

my data

I am using set() to write the data. Now, I want the user to edit their bookTitle and it needs to change on both places. I tried using update() but I can´t seem to make it work. I can only edit the bookTitle in bookInfo NOT on books.

Moving is not an option because it will erase the bookData. I also tried writing using push() but then, I can´t search properly because I don´t have the pushID (I need the search because users can't have two books with the same name)

So, is there a way to update the key value? or, is there a better approach to this? I accept suggestions. Thank you!

Update: This is what I´m currently using to update the book title inside bookInfo

var bookName = document.getElementById('bookName').value;  firebase.database().ref('books/' + bookName + '/bookInfo').update({     bookTitle : bookName }); 
like image 979
Luis Rodriguez Avatar asked Aug 23 '16 17:08

Luis Rodriguez


People also ask

Is there primary key in Firebase?

This chat application allows users to store the basic profile and contact list. The user profile would be located on a path such as Users/$uid. User is a node in it and will have a sort of primary key associated with an ID. So, we can access each one uniquely.

Is Firebase key value?

In Firebase Database everything is a node, that follows the pattern key: value. Firebase Database provides us with a simple way to generate unique keys. Unique keys create new items while uploading data to a previously stored key will update.

How do I get a key in Firebase?

Creating API keys Firebase automatically creates API keys for your project when you do any of the following: Create a Firebase project > Browser key auto-created. Create a Firebase Apple App > iOS key auto-created. Create a Firebase Android App > Android key auto-created.

Which is faster Realtime Database or firestore?

Cloud Firestore also features richer, faster queries and scales further than the Realtime Database.


2 Answers

I think I see what you're trying to do. Firebase doesn't have the concept of "renaming" a part of the path via update. Instead you will have to completely remove the existing node and recreate it. You can do that like so:

var booksRef = firebase.database().ref('books'); booksRef.child(oldTitle).once('value').then(function(snap) {   var data = snap.val();   data.bookInfo.bookTitle = newTitle;   var update = {};   update[oldTitle] = null;   update[newTitle] = data;   return booksRef.update(update); }); 

This will remove the info from books/oldTitle and re-populate it with a new title in books/newTitle.

Caveat: This relies on reading the data and then performing a second async update. If you are likely to have multiple users operating on the same data at the same time this could cause issues. You could use a transaction to do this atomically but if /books is a top-level resource with many nodes that may cause performance problems.

If one person is likely to edit the data at a time, the above solution is fine. If not, you may want to consider using a non-user-controlled identifier such as a push id.

like image 131
Michael Bleigh Avatar answered Oct 28 '22 22:10

Michael Bleigh


You can export all the database as JSON, rename it in your favorite editor import it again to firebase via import/export option on the top right in the console windowenter image description here

like image 40
Rami Avatar answered Oct 28 '22 22:10

Rami