Can I change the name of a document in Firestore?

后端 未结 1 1769
臣服心动
臣服心动 2021-01-04 00:11

I\'m currently working on an app where I have to retrieve data from Google\'s Firestore.

My data structure looks like this:

users
 - name@xxx.com
            


        
相关标签:
1条回答
  • 2021-01-04 00:46

    I think the best way to do this is to get the data from 'name@xxx.com' and upload it to a new document called 'name' and then delete the old one.

    Just as an example:

    const firestore = firebase.firestore();
    // get the data from 'name@xxx.com'
    firestore.collection("users").doc("name@xxx.com").get().then(function (doc) {
        if (doc && doc.exists) {
            var data = doc.data();
            // saves the data to 'name'
            firestore.collection("users").doc("name").set(data).then({
                // deletes the old document
                firestore.collection("users").doc("name@xxx.com").delete();
            });
        }
    });
    
    0 讨论(0)
提交回复
热议问题