【发布时间】:2020-12-01 00:04:29
【问题描述】:
是否可以在 DynamoDB 中对项目进行批量更新插入,类似于 MongoDB 批量写入的方式?
我在 MongoDB 中有这个函数,希望在 DynamoDB 中做类似的事情:
async bulkSync(
model: any | null,
context: any[],
filterKeys: Array<string> = ['_id'],
): Promise<any> {
let operations = []; // Initialize the bulk operations array
let i = 1;
for (const record of context) {
const filter: any = {};
for (const key of filterKeys) {
const path = key.split('.');
const value = R.path(path, record);
filter[key] = value;
}
operations.push({
updateOne: {
filter: filter,
update: {
$set: record,
$setOnInsert: {
created_at: record.updated_at,
},
},
upsert: true, // example update operation,
setDefaultsOnInsert: true,
},
});
if (operations.length % 500 === 0 || i === context.length) {
await model.collection.bulkWrite(operations, {
ordered: true,
w: 1,
});
operations = [];
}
i++;
}
return;
}
【问题讨论】:
标签: node.js mongodb amazon-dynamodb nestjs