【问题标题】:Batch write to firebase cloud firestore批量写入 Firebase Cloud Firestore
【发布时间】:2018-08-03 02:58:14
【问题描述】:

我想创建一个新集合并向其中添加数千个大小约为 1-2K 的文档。我已经在 json 中有数据,所以我认为这很容易。

我知道批处理一次可以有 500 次写入,因此为了将其分成 500 次的块,我编写了以下代码。虽然出于测试目的,我使用 20 个块运行它,而我的测试 json 有 72 个对象。

但我不断收到以下错误

node_modules\@google-cloud\firestore\src\write-batch.js:148
  throw new Error('Cannot modify a WriteBatch that has been committed.');
  ^

Error: Cannot modify a WriteBatch that has been committed.

我的代码如下

var dataObj = JSON.parse(fs.readFileSync('./bigt.json'))
var tmpdd = dataObj.slice(0, 72)
var batch = db.batch();

console.log(tmpdd.length)

let tc = tmpdd.length
let lc = 0
let upperLimit = 20, dd = null

while(lc<=tc){

    dd = tmpdd.slice(lc, upperLimit )

    console.log(lc, upperLimit)
    dd.map(
    o => batch.set(db.collection('nseStocks').doc(o.Date+o.variable), o)
    )

    batch.commit().then(function () {
        console.log('Written to firestore', lc, lc + upperLimit)
    })
    .catch(
        (err) => console.log('Fail', err)
    )

    lc = upperLimit
    upperLimit = upperLimit + 20 

}

此外,在循环的每次迭代中似乎都没有提交批处理,这很奇怪。理想情况下,我会让 Firestore 确定文档 ID,但显然批处理没有添加功能。

我尝试在循环中添加文档而不是批量写入。但是在添加一些文档后它给了我超时错误。当然,这对于大量文档来说是不切实际的。

你可以看出我对 Firestore 很陌生,这是我玩 Firestore 的第二天。

如果有任何明显的错误或更好的方法来完成这个看似简单的任务,请告诉我。

谢谢

【问题讨论】:

    标签: node.js firebase google-cloud-firestore


    【解决方案1】:

    这对我有用:

    function loadJson(){
        var ref = firebase.firestore().collection("my-parent-collection-name");
        getJSON("https://my-target-domain.com/my-500-plus-objects-file.json").then(function (data) {
            var counter = 0;
            var commitCounter = 0;
            var batches = [];
            batches[commitCounter] = db.batch();
            Object.keys(data).forEach(function(k, i) {
                if(counter <= 498){
                    var thisRef = ref.doc(k);
                    batches[commitCounter].set(thisRef, data[k]);
                    counter = counter + 1;
                } else {
                    counter = 0;
                    commitCounter = commitCounter + 1;
                    batches[commitCounter] = db.batch();
                }
            });
            for (var i = 0; i < batches.length; i++) {
                batches[i].commit().then(function () {
                    console.count('wrote batch');
                });
            }
        }, function (status) {
            console.log('failed');
        });
    
    }
    loadJson();
    

    【讨论】:

      【解决方案2】:

      您正在为程序顶层的所有写入创建一个批处理。它被重用于您为所有批量写入所做的所有对 batch.set() 的调用。

      var batch = db.batch();
      

      相反,您应该为每组写入创建一个新批次。您可以在 while 循环的顶部执行此操作:

      while(lc<=tc) {
          var batch = db.batch();
          // use the new batch here
      }
      

      【讨论】:

        猜你喜欢
        • 2021-03-31
        • 1970-01-01
        • 2018-07-04
        • 1970-01-01
        • 2021-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-27
        相关资源
        最近更新 更多