cbdeveloper
cbdeveloper

Reputation: 31425

Can I batch together an add() and a set() operation in cloud Firestore?

From Firebase docs, we get that:

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.

But in my case, I need to be sure that an add() operation (creating a new document) will happen together with a set() operation to update some other pre-existing document.

Is there a way to do this?

Note: I'm using the Javascrip SDK.

Upvotes: 2

Views: 3238

Answers (3)

RameshD
RameshD

Reputation: 962

In order to create new document equivalent to add() using batch writes we need to first generate the uid through use of createID() function from angularfirestore and set batch.

However, updating the existing document is pretty straight forward. We simply provide its uid while setting the batch.

`

constructor(
    private angularFireStore: AngularFirestore,
   ) {}

 const batch = this.angularFireStore.firestore.batch();

 // generates the unique uid of 20 chars 
 const autogenUid = this.angularFireStore.createId();
 // this is new doc ref with newly generated uid
 const collectionReference = this.angularFireStore.collection
                            ('collection_name').doc(autogenUid).ref;

 const docData = {first_field:'value', second_field:'value2'};
 batch.set(collectionReference, docData);

 // to update existing , we simply need to know its uid beforehand, 
 const collection2Reference = this.angularFireStore.collection
                            ('collection2_name').doc('existingUid').ref;

 const docData2 = {first_field:'value', second_field:'value2'};
 batch.set(collection2Reference, docData2);

 batch.commit();

Upvotes: 1

andresmijares
andresmijares

Reputation: 3744

If you do

 const batch = firestore().batch()
 const sampleRef = firestore().collection(‘sample’)
 const id = sampleRef.doc().id
 batch.set(sampleRef.doc(id), {...})
 batch.commit()

It should do thé trick, its thé same as add

Upvotes: 8

Renaud Tarnec
Renaud Tarnec

Reputation: 83113

Just use the CollectionReference's doc() method followed by a call to the BatchedWrite's set() method, in order to "mimic" a call to the add() method,

Excerpt from https://firebase.google.com/docs/reference/js/firebase.firestore.CollectionReference#doc:

If no path is specified, an automatically-generated unique ID will be used for the returned DocumentReference.

Therefore, in your batch you can do:

// Get a new write batch
var batch = db.batch();

// A "standard" Set
var nycRef = db.collection("cities").doc("NYC");
batch.set(nycRef, {name: "New York City"});

// A Set that is similar to an Add
var unknownCityRef = db.collection("cities").doc();
batch.set(unknownCityRef, {name: "Unknown City"});

// Commit the batch
batch.commit().then(function () {
    // ...
});

Upvotes: 2

Related Questions