The documentation for Firestore batch writes lists only set()
, update()
and delete()
as permitted operations.
Is there no way to add an add()
operation to the batch? I need a document to be created with an auto-generated id.
Each transaction or batch of writes can write to a maximum of 500 documents.
A subcollection is a collection associated with a specific document. Note: You can query across subcollections with the same collection ID by using Collection Group Queries. You can create a subcollection called messages for every room document in your rooms collection: collections_bookmark rooms.
You can do this in two steps:
// Create a ref with auto-generated ID var newCityRef = db.collection('cities').doc(); // ... // Add it in the batch batch.set(newCityRef, { name: 'New York City' });
The .doc()
method does not write anything to the network or disk, it just makes a reference with an auto-generated ID you can use later.
In my case, using AngularFire2, I had to use the batch.set() method, passing as first parameter the document reference with an ID previously created, and the reference attribute:
import { AngularFirestore } from '@angular/fire/firestore'; ... private afs: AngularFirestore ... batch.set( this.afs.collection('estados').doc(this.afs.createId()).ref, er.getData() );
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