我想要一个示例代码将文档同时添加到多个Firestore集合中
这是我的Firestore结构的一个例子。
{
"users": {
"UUID-1": {
"list":[
"text1",
"text2",
"text3",
"text4",
...
]
},
"UUID-2": {
"list":[
"text1",
"text2",
"text3",
"text4",
...
]
}
...
}
}我想在list[]中输入用户中所有用户的文本
我阅读了这篇文章,并编写了以下代码。https://firebase.google.com/docs/firestore/manage-data/transactions#batched-writes
let batch = db.batch();
batch.set(
db.collection('users').doc().collection(list),
{
unread: newUserId
}
);
batch.commit();发布于 2019-11-08 09:54:55
您可以将多个写入操作作为包含两个
set()操作的单个批处理(或事务)执行。。
来自 文档 (我自己还没有正式测试过):
// Get a new write batch
let batch = db.batch();
// Set the value of 'John Doe'
let johnRef = db.collection('users').doc('UUID-1').collection('list');
batch.set(nycRef, {name: 'John Doe'});
// Set the value of 'Mary Thompson'
let maryRef = db.collection('users').doc('UUID-2').collection('list');
batch.set(maryRef, {name: 'Mary Thompson'});
// Commit the batch
return batch.commit().then(function () {
// ...
});希望这能有所帮助。
https://stackoverflow.com/questions/58763087
复制相似问题