I want to move a doc from one collection to another. Therefore I want to use a transaction to 1. create the new doc and 2. delete the old one.
I can do the following, which works:
db.runTransaction((transaction) => {
return transaction.get(docRef)
.then(() => transaction.set(newDocRef.doc(docId), doc))
.then(() => transaction.delete(docRef));
How can I rewrite this code to start with a transaction.set()
instead of a transaction.get()
since I already have the doc in this context so its redundant. The difference is that transaction.get()
returns a promise whereas transaction.set()
returns a transaction
.
Well if you do not want to read the document, you should be using Batched Writes.
Firestore Docs:https://firebase.google.com/docs/firestore/manage-data/transactions
Batched writes
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With