Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to rename fields in firestore collection?

I am new to Cloud Firestore in Firebase. In my project I have created collections but I need to rename field names for each collection. Is it possible? Any help will be highly appreciated.

like image 681
hytsan Avatar asked Jul 12 '19 09:07

hytsan


People also ask

Can you rename document in Firebase?

Right-click on the left-most field (ID column) of the document and choose Rename Document.

What is the difference between collection and document in firestore?

Documents within the same collection can all contain different fields or store different types of data in those fields. However, it's a good idea to use the same fields and data types across multiple documents, so that you can query the documents more easily. A collection contains documents and nothing else.

How do I rename my firestore document ID?

Another alternative is to enter the document path directly in the Path field, e.g. myCollection/myDocumentId and click the Run button. Right-click on the left-most cell (ID column) of the document and choose Rename Document. Change the last part of the Target Path to the new document name. That's it!

How many fields can a firestore document have?

Because each field appears in 2 indexes (ascending and descending), the maximum number of fields is 20,000.


2 Answers

Is it possible to rename fields in firestore collection?

No, it's not, there is no API for doing that. In Firestore it's not possible to change the names of your properties once you have created them. All that fields are immutable.

A workaround would be to read your documents, make the necessary changes and then put it all back in with the new property names.

like image 50
Alex Mamo Avatar answered Sep 27 '22 20:09

Alex Mamo


I wrote few helpers for firestore: https://github.com/39ro/fireboost

One of those include renaming a field for a document

const docRef = db.collection('users').doc('xyz');
const utilDocRef = fireBoostFirestore.ref(docRef)

utilDocRef.renameField({oldFieldKey: 'newFieldKey'})

or for all documents in a collection

const colRef = db.collection('users');
const utilColRef = fireBoostFirestore.ref(colRef);

utilColRef.renameFieldDocs({oldFieldKey: 'newFieldKey'})

Hope it can help someone!

like image 33
39ro Avatar answered Sep 27 '22 18:09

39ro