Add new field or change the structure on all Firestore documents

前端 未结 5 709
感动是毒
感动是毒 2021-02-02 13:11

Consider a collection of users. Each document in the collection has name and email as fields.

{
  \"users\": {
    \"uid1\         


        
5条回答
  •  花落未央
    2021-02-02 13:21

    Just wanted to share because I read that you were hoping for a Firestore based solution.

    This worked for me. forEach will query each document in the collection and you can manipulate as you like.

        db.collection("collectionName").get().then(function(querySnapshot) {
            querySnapshot.forEach(async function(doc) {
                await db.collection("collectionName").doc(doc.id).set({newField: value}, {merge: true});
                // doc.data() is never undefined for query doc snapshots
                console.log(doc.id, " => ", doc.data());
            });
        });

提交回复
热议问题