I have a large collection of documents stored in Firebase. They were all stored with a number of fields and given the randomly generated Document ID. I neglected to add a field titled "id" to populate with the Document ID, so now I need a way to first create the field and then populate it with the Document ID. I am completely new to this, so I don’t even know where to begin or if it’s even possible, and I don’t even know where to run the code. Thank you for any help!
Other than trying to find answers via Google, I haven’t tried anything. I found answers to other questions that were close to my question, but nothing that solved my problem.
2
Answers
below is block in js as you did not specified anything.
but try to understand the following
going through the following might be helpful to you
add-data#web-namespaced-api_9 and queries
I cannot see a reason for doing need that. You can access a document ID even if it’s not stored in the document itself. When you perform a query against a Firestore collection, you get back documents along with their IDs. There is no need to have the document IDs nested inside the document.
Furthermore, if you want to map a document into an object of a specific class, but the class doesn’t contain a field for the document ID, then you should simply add it. When you perform the query, the value of the field will always be
null
, since the document doesn’t contain any ID. What you have to do, is to callgetId()
on aQuerySnapshot
object and assign the result to the newly created field. In this way, you’ll have an object of the class that contains all the data together with the document ID.If you however want to do that, please note that you cannot do that in a single step. You’ll have to read all documents in the collection and then perform a write operation to update the document. This also means that you have to pay for both read and write operations, without having a real benefit.