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

Dr.*_*YSG 4 mongodb azure-cosmosdb

我正在尝试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
Run Code Online (Sandbox Code Playgroud)

以下是连接机制、架构和最初填充集合的代码。

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()
    }
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*gon 6

您收到的错误16500是“请求过多”。TL;DR 您已达到 RU 限制并且正在受到限制。每次插入都会消耗一定程度的 RU,并且您的每秒 RU 速率设置得不够高,无法处理您所设置的快速插入场景。

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

仅供参考,我只是通过db.collection.insertMany()从 mongo shell 调用大量文档(在本例中为大约 150 个小文档)以及非常低的 RU 计数(在我的例子中为 400)来重新创建此错误情况。