我想创建一个新集合,并向其中添加成千上万个大小约为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.
Run Code Online (Sandbox Code Playgroud)
我的代码如下
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( …Run Code Online (Sandbox Code Playgroud)