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

hyt*_*san 7 firebase google-cloud-firestore

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

Ale*_*amo 9

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

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

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


39r*_*9ro 7

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

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

const docRef = db.collection('users').doc('xyz');
const utilDocRef = fireBoostFirestore.ref(docRef)

utilDocRef.renameField({oldFieldKey: 'newFieldKey'})
Run Code Online (Sandbox Code Playgroud)

或对于集合中的所有文档

const colRef = db.collection('users');
const utilColRef = fireBoostFirestore.ref(colRef);

utilColRef.renameFieldDocs({oldFieldKey: 'newFieldKey'})
Run Code Online (Sandbox Code Playgroud)

希望它可以帮助某人!