Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore - batch.add is not a function

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.

like image 298
artooras Avatar asked Oct 13 '17 08:10

artooras


People also ask

What is the maximum documents that are write by per transaction or batch of write in cloud firestore?

Each transaction or batch of writes can write to a maximum of 500 documents.

What is a Subcollection in firestore?

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.


2 Answers

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.

like image 157
Sam Stern Avatar answered Sep 29 '22 10:09

Sam Stern


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()   ); 
like image 21
Sebastián de Prada Gato Avatar answered Sep 29 '22 11:09

Sebastián de Prada Gato