Wan*_*lle 16 firebase google-cloud-firestore
我正面临来自Firebase的新Firestore的问题.
情况:我有一个collection('room')
我创造了房间 collection('room').add(room)
我正在尝试做什么:我需要更新一个房间.
为此,我使用: collection('room').doc(ROOM_ID).update(update)
所以我需要在我的集合中的文档中添加ROOM_ID:
|room
ROOM_ID
id:ROOM_ID,
someContent: ForTheQuery
Run Code Online (Sandbox Code Playgroud)
有可能实现这一目标吗?
另一种方法是使用以下命令创建自己生成的ID:
collection('room')
.doc(someId)
.set({
id: someId,
someContent: ForTheQuery
});
Run Code Online (Sandbox Code Playgroud)
但我想避免它.
Dou*_*son 41
您可以使用doc()创建对具有唯一ID的文档的引用,但不会创建该文档.然后,您可以使用文档参考中提供的唯一ID来设置该doc的内容:
const ref = store.collection('users').doc()
console.log(ref.id) // prints the unique id
ref.set({id: ref.id}) // sets the contents of the doc using the id
.then(() => { // fetch the doc again and show its data
ref.get().then(doc => {
console.log(doc.data()) // prints {id: "the unique id"}
})
})
Run Code Online (Sandbox Code Playgroud)
ANGULARFIRE:
在添加数据库之前获取ID:
var idBefore = afs.createId();
console.log(idBefore );
Run Code Online (Sandbox Code Playgroud)
ANDROID FIRESTORE:
String idBefore = db.collection("YourCol").document().getId();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18809 次 |
| 最近记录: |