我想将文档从一个集合移动到另一个集合。因此,我想使用事务来 1. 创建新文档和 2. 删除旧文档。
我可以执行以下操作:
db.runTransaction((transaction) => {
return transaction.get(docRef)
.then(() => transaction.set(newDocRef.doc(docId), doc))
.then(() => transaction.delete(docRef));
我如何重写此代码以从 transaction.set()
而不是 transaction.get()
开始,因为我已经有了这个上下文中的文档,所以它是多余的。区别在于 transaction.get()
返回一个 promise ,而 transaction.set()
返回一个 transaction
。
最佳答案
如果你不想阅读文档,你应该使用 Batched Writes。
Firestore Docs: https://firebase.google.com/docs/firestore/manage-data/transactions
批量写入
If you do not need to read any documents in your operation set, you can execute multiple write operations as a single batch that contains any combination of set(), update(), or delete() operations. A batch of writes completes atomically and can write to multiple documents.
关于javascript - 在没有 transaction.get() 的情况下启动 firestore 事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49252227/