是否可以仅使用一个请求在Firestore中存储多个文档?使用此循环可能但这会导致列表中的每个项目进行一次保存操作.
for (counter in counters) {
val counterDocRef = FirebaseFirestore.getInstance()
.document("users/${auth.currentUser!!.uid}/lists/${listId}/counters/${counter.id}")
val counterData = mapOf(
"name" to counter.name,
"score" to counter.score,
)
counterDocRef.set(counterData)
}
Run Code Online (Sandbox Code Playgroud) 有没有办法可以使用Firestore获取作为批处理的一部分创建的文档的自动生成的ID?
使用时.add()我可以很容易地获得一个ID:
db.collection('posts')
.add({title: 'Hello World'})
.then(function(docRef) {
console.log('The auto-generated ID is', docRef.id)
postKey = docRef.id
});
Run Code Online (Sandbox Code Playgroud)
使用.batch()我可以使用.doc()和添加文档.set():
const batch = db.batch();
const postsRef = db.collection('posts').doc(postKey);
batch.set(postsRef, {title: 'Hello Again, World'});
const votesRef = db.collection('posts').doc(postKey)
.collection('votes').doc();
batch.set(votesRef, {upvote: true})
batch.commit().then(function() {
});
Run Code Online (Sandbox Code Playgroud)
我可以获取添加到的文档的自动生成的ID votes吗?
更新:
Doug Stevenson是正确的 - 我可以访问ID votesRef.id
在 firestore v8 (web sdk) 中我是这样做的,如下所示:
firestore.ref().collection("genId").doc().id
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚 v9 的正确语法,因为collection不再有doc()方法了。
非常感谢任何建议