skip to Main Content

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


  1. below is block in js as you did not specified anything.

    but try to understand the following

    db.collection("cities").where("capital", "==", true) //<-- pass what ever type of query you have 
                                                        // if not a special query just leave it like
                                                        //db.collection("cities").get().then( ...
        .get()
        .then((querySnapshot) => {
            querySnapshot.forEach(async (doc) => {
                // doc.data() is never undefined for query doc snapshots
                console.log(doc.id, " => ", doc.data());
            
                var docIdString=doc.id+'';
                var washingtonRef = await db.collection("cities").doc(docIdString);
    
    
    return washingtonRef.set({
        id: docIdString //capital: true
      }, { merge: true }) //<-- use merge =true so that only the given field will be added to the doc
    .then(() => {
        console.log("Document successfully updated!");
    })
    .catch((error) => {
        // The document probably doesn't exist.
        console.error("Error updating document: ", error);
    });
            });
        })
        .catch((error) => {
            console.log("Error getting documents: ", error);
        });
    

    going through the following might be helpful to you

    add-data#web-namespaced-api_9 and queries

    Login or Signup to reply.
  2. 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 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 call getId() on a QuerySnapshot 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search