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.
Right-click on the left-most field (ID column) of the document and choose Rename Document.
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.
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!
Because each field appears in 2 indexes (ascending and descending), the maximum number of fields is 20,000.
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.
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!
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