Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firestore事务是否可以批处理?_Javascript_Google Cloud Firestore - Fatal编程技术网

Javascript Firestore事务是否可以批处理?

Javascript Firestore事务是否可以批处理?,javascript,google-cloud-firestore,Javascript,Google Cloud Firestore,我对firestore集合查询执行了一系列更新,其中每个更新都需要访问当前值。我知道事务是执行这些更新的原子方式,但是是否可以批处理这一系列更新 目前,我更新集合的方式如下: let query; query = db.collection('focuses'); query = query.where('userId', '==', auth.currentUser.uid); query = query.where('active', '==', true); query.get().th

我对firestore集合查询执行了一系列更新,其中每个更新都需要访问当前值。我知道事务是执行这些更新的原子方式,但是是否可以批处理这一系列更新

目前,我更新集合的方式如下:

let query;
query = db.collection('focuses');
query = query.where('userId', '==', auth.currentUser.uid);
query = query.where('active', '==', true);

query.get().then(snapshot => {
  const batch = db.batch();

  snapshot.forEach(doc => {
    batch.update(
      db.collection('focuses').doc(doc.id),
      { 
        active: false, 
        working: true,
        time: doc.data().workPeriod * 60,
      }
    );
  });

  batch.commit().then(() => {
    auth.signOut().catch(error => {
      console.error(error);
    });
  }).catch(error => {
    console.error(error);
  });
});
这是可行的,但是
workPeriod
字段是在事务之外访问的,因此我的理解是,它可能容易同时被访问


那么,是否可以为每个批处理更新执行一个事务?如果不是,那么最好的方法是从一系列事务中收集每个
承诺,然后使用
Promise.all
设置回调,在一系列事务完成时调用该回调?

否,事务不能批处理。您必须为原子更新选择批处理或事务。事务类似于批处理,因为所有更新都适用,或者都不适用。批处理和事务之间的区别在于批处理只是写入,而事务先读后写。

不,事务不能批处理。您必须为原子更新选择批处理或事务。事务类似于批处理,因为所有更新都适用,或者都不适用。批处理和事务之间的区别在于批处理只是写,而事务先读后写