Can I change the name of a document in Firestore?

你离开我真会死。 提交于 2020-11-27 04:54:37

问题


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
    - more data

Is there a way for me to change the name@xxx.com to just name? I can't see a method to change the name of a document.


回答1:


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();
        });
    }
});


来源:https://stackoverflow.com/questions/47885921/can-i-change-the-name-of-a-document-in-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!