面试题答案
一键面试// 使用updateMany方法更新符合条件的文档
db.data.updateMany(
{ "complexObj.nestedObj.list.key2": { $gt: 15 } },
[
{
$addFields: {
"complexObj.nestedObj.list": {
$map: {
input: "$complexObj.nestedObj.list",
as: "item",
in: {
$cond: [
{ $gt: ["$$item.key2", 15] },
{ key1: { $toUpper: "$$item.key1" }, key2: "$$item.key2" },
"$$item"
]
}
}
}
}
}
]
);
// 查询修改后的文档
db.data.find();