【发布时间】:2025-12-25 23:55:16
【问题描述】:
我有一个猫鼬模式定义为
const masterSchema = new mongoose.Schema({
chapter: { type: Number, required: true },
line: { type: Number, required: true },
translations: [
{
translation: { type: String, required: true },
},
],
});
我正在尝试从 CSV 文件更新集合。该集合有超过 5000 个文档。
样本数据
[
{
chapter: 1,
line: 1,
translations: [
{
translation: "xyz",
},
],
},
{
chapter: 1,
line: 2,
translations: [
{
translation: "abc",
},
],
},
];
CSV 文件的格式为
chapter,line,translation
1,1,example1
1,2,example2
....
输出应该是
[
{
chapter: 1,
line: 1,
translations: [
{
translation: "xyz",
},
{
translation : "example1"
}
],
},
{
chapter: 1,
line: 2,
translations: [
{
translation: "abc",
},
{
translation : "example2"
}
],
},
]
我对如何使用 updateMany() 将数据插入正确的文档感到困惑。 (如果它是解决问题的正确方法)
【问题讨论】:
-
updateMany表示您更新了多个文档。updateOne表示您更新了一个文档,无论您更新了多少字段或数组有多少元素。
标签: javascript mongodb mongoose mongodb-query aggregation-framework