【问题标题】:CosmosDB (mongo/mongoose) Bulk Write ErrorCosmosDB (mongo/mongoose) 批量写入错误
【发布时间】:2019-12-05 15:52:01
【问题描述】:

我正在尝试使用 insertMany() 将 50 个文档放入一个集合中。在本地模拟器上,这工作正常。当我在 Azure 中使用 CosmosDB 尝试相同的代码时,我收到以下错误:

2019-12-05T00:12:51.320 [Information] Connected to Cosmsos DB
2019-12-05T00:12:51.576 [Error] failed to create users, err: BulkWriteError: Error=16500, RetryAfterMs=72, Details='
2019-12-05T00:12:51.320 [Information] Connected to Cosmsos DB
2019-12-05T00:12

这里是连接机制、模式和最初填充集合的代码。

const personSchema = new mongoose.Schema({
    firstName: String,
    lastName: String,
    addressNumber: Number,
    streetName: String,
    city: String,
    email: String
})

async open() {
    let host = this.conn
    this.log.info(`Host: ${JSON.stringify(host)}`)
    const url = `mongodb://${host.host}:${host.port}/${host.db}?ssl=true&replicaSet=globaldb&retryWrites=false`
    const options = {
        auth: {
            user: host.user,
            password: host.password
        }
    }
    try {
        await mongoose.connect(url, options)
        this.log.info('Connected to Cosmsos DB')
        this.db = mongoose
    } catch (err) {
        this.log.error('Unable to connect to DB', err)
    }
    this.users = mongoose.model('person', personSchema)
}

async populate(logger) {
    await this.connect(logger)
    try {
        await this.users.deleteMany({})
    } catch (err) {
        this.log.error(`cannot empty collection: ${err}`)
    }
    const uList = userData.map(u => this.users(u))
    try {
        const result = await this.users.collection.insertMany(uList)
        this.log.info(`users created: ${result}`)
    } catch (err) {
        this.log.error(`failed to create users, err: ${err}`)
    } finally {
        await this.close()
    }
}

【问题讨论】:

标签: mongodb azure-cosmosdb


【解决方案1】:

16500 您遇到的错误是“请求过多”。 TL;DR 你已经达到了你的 RU 限制并且被限制了。每个插入都会消耗一定程度的 RU,而您的每秒 RU 速率设置得不够高,无法处理像您设置的那样的快速插入场景。

您可以通过增加 RU 容量或减少同时尝试插入的项目数量来解决此问题。

仅供参考,我刚刚通过从 mongo shell 调用 db.collection.insertMany() 重新创建了这个错误条件,其中包含大量文档(在本例中,大约 150 个小文档),以及非常低的 RU 计数(在我的例子中为 400 个)。

【讨论】:

  • 批量插入有帮助吗?不太清楚它是如何调用的。我有 50 个小文件。我认为 400 RU 就足够了。
  • 文档似乎表明在 3.6 中,只要使用数组即可。 insertMany 与批量写入相同。我想增加 RU 是唯一的方法。
猜你喜欢
  • 2015-08-02
  • 1970-01-01
  • 1970-01-01
  • 2020-07-05
  • 2013-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多