最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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 bination of set(), update(), or delete() operations. A batch of writes pletes 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.

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 bination of set(), update(), or delete() operations. A batch of writes pletes 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.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jun 21, 2019 at 10:28 cbdevelopercbdeveloper 31.5k44 gold badges198 silver badges396 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

If you do

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

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

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./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.mit().then(function () {
    // ...
});

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.mit();
发布评论

评论列表(0)

  1. 暂无评论