在消防仓库批量写作,我可以在父母之前给孩子写吗



我的应用程序解析一些JSON并创建如下数据:

School -- has several sub collections...
|
Classrooms -- about 25 docs
|
Staff -- about 30 docs
|
Families -- ~350 docs, each with sub collections...
|
Parents -- a few docs, usually 2
|
Students -- a few docs, usually 1 or 2

总共有大约1500个文档,我使用以下代码在几个批处理集中编写(对于creates,它只使用"set"(:

// this.batchOps is a large array of:
// [ { operation:'set', ref: firestore ref, data: { simple object }, ...]
performBatchOps () {
if (this.batchOps.length === 0) return Promise.resolve()
const batchSize = 200 // I realize this may be as high as 500
let batch = db.batch();
this.batchOps.slice(0, batchSize).forEach(s => {
if (s.operation === 'set') batch.set(s.ref, s.data)
else if (s.operation === 'update') batch.update(s.ref, s.data)
else if (s.operation === 'delete') batch.delete(s.ref)
})
return batch.commit().then(() => this.performBatchOps(this.batchOps.slice(batchSize)))
}

我有两个版本的代码。一个成功可靠,一个失败可靠。这两个版本之间的唯一区别是,成功的版本在批次之前创建了学校(所有东西的家长(,而失败的版本在批次内创建了学校。。。

// works
return schoolRef.set(schoolData).then(() => {
this.batchOps = this.parseJSON(someJson)
return this.performBatchOps()  // these batch ops don't contain a set for the school
}) 
// breaks
let batchOps = this.parseJSON(someJson) // the first batch op sets the school
return performBatchOps()

我想我理解批处理写入不能保持顺序,我想我也理解创建一个文档,比如:database/x/y/z/document,可以在创建它上面的树之前(写入它隐含地写入它的沿袭(。

故障在日志中显示为Error: 10 ABORTED: Too much contention on these documents. Please try again.,如本问题所述(当我认为问题与创建时运行的触发器有关时(。

我知道我必须先更改代码才能编写学校文档,但我是否也必须重新安排孩子的编写(在批处理之外设置整棵树,只在批处理中编写叶子(?

我想争论的错误与学校文档上的争论有关。在日志中有没有我可以/可能发现的地方?这将为我节省大量的部署测试修复周期。

无论父/子关系如何,批处理中的操作顺序都无关紧要。没有父文档,子文档也可以很好地存在——除了如何定义它们的关系之外,它们彼此完全无关。

错误消息告诉你,你在一个文档上写得太多太快,可能是你提到的学校文档。根据文档,单个文档的写入限制为每秒1次。你可以在短时间内超过这个数字,但它不可能持续下去。

最新更新