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

4

我试图使用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()
    }
}

这个回答解决了你的问题吗?CosmosDb Request rate is large with insertMany - David Makogon
1个回答

6
你收到的错误代码是 "16500",意思是 "请求过多"。简单来说,你已经超过了RU限制并正在被限制速度。每次插入都会消耗一定数量的RU,而你的每秒RU速率不足以处理像你所设置的快速插入场景。
要解决这个问题,你可以增加 RU 容量或者减少同时插入的项目数量。
顺带提一下,我刚刚通过在mongo shell中调用 db.collection.insertMany() 并使用大型文档数组(在本例中,大约150个小文档)以及非常低的RU计数(在我的情况下为400)重新创建了这个错误条件。

批量插入会有帮助吗?不太清楚如何调用它。我有50个小文档。我认为400 RU足够了。 - Dr.YSG
文档似乎表明,在3.6中,只要使用数组,insertMany就与批量写入相同。我想增加RU是唯一的方法。 - Dr.YSG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接