CosmosDB(mongo / mongoose)批量写入错误

时间:2019-12-05 15:52:01

标签: mongodb azure-cosmosdb

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

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()
    }
}

1 个答案:

答案 0 :(得分:1)

您收到的错误16500是“请求太多”。 TL; DR您已达到RU限制并受到限制。每个插入都会花费一定程度的RU,并且您的每秒RU速率设置得不够高,无法处理您所设置的快速插入方案。

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

仅供参考,我刚刚通过从mongo shell中调用db.collection.insertMany()来重新创建此错误条件,该文件包含大量文档(在这种情况下,大约150个小文档),并且RU计数非常低(本例中为400) )。