相关疑难解决方法(0)

如何在Firestore中一次创建/更新多个文档

是否可以仅使用一个请求在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)

android firebase google-cloud-firestore

35
推荐指数
2
解决办法
2万
查看次数

我可以使用firestore获取使用batch()创建的文档的生成ID吗?

有没有办法可以使用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

javascript firebase google-cloud-firestore

13
推荐指数
1
解决办法
5000
查看次数

在创建文档之前在 firestore v9 中生成文档 ID

在 firestore v8 (web sdk) 中我是这样做的,如下所示:

firestore.ref().collection("genId").doc().id
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚 v9 的正确语法,因为collection不再有doc()方法了。

非常感谢任何建议

javascript firebase google-cloud-firestore

5
推荐指数
1
解决办法
2431
查看次数