【问题标题】:Is it possible to rename fields in firestore collection?是否可以重命名 Firestore 集合中的字段?
【发布时间】:2019-11-22 01:05:06
【问题描述】:

我是 Firebase 中 Cloud Firestore 的新手。在我的项目中,我创建了集合,但我需要为每个集合重命名字段名称。可能吗?任何帮助将不胜感激。

【问题讨论】:

    标签: firebase google-cloud-firestore


    【解决方案1】:

    是否可以重命名 firestore 集合中的字段?

    不,不是,没有 API 可以做到这一点。在 Firestore 中,一旦您创建了属性,就无法更改它们的名称。所有这些字段都是不可变的。

    一种解决方法是阅读您的文档,进行必要的更改,然后使用新的属性名称将其全部放回。

    【讨论】:

      【解决方案2】:

      我为 firestore 写了几个助手:https://github.com/39ro/fireboost

      其中之一包括重命名文档的字段

      const docRef = db.collection('users').doc('xyz');
      const utilDocRef = fireBoostFirestore.ref(docRef)
      
      utilDocRef.renameField({oldFieldKey: 'newFieldKey'})
      

      或对于集合中的所有文档

      const colRef = db.collection('users');
      const utilColRef = fireBoostFirestore.ref(colRef);
      
      utilColRef.renameFieldDocs({oldFieldKey: 'newFieldKey'})
      

      希望它可以帮助某人!

      【讨论】:

        猜你喜欢
        • 2017-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-05
        • 2016-05-16
        • 1970-01-01
        • 2019-01-08
        相关资源
        最近更新 更多