You can run aggregation pipeline to do this on server side:
db.testcol.aggregate([
{
//only take docs with wrong field name
$match: {
"arrField.wrong name": { $exists: true },
},
},
//unwind so you can then group again with correct field name
{ $unwind: "$arrField" },
//project your field name correctly, alongwith other fields
{
$project: {
_id: "$_id",
"arrField.correctName": "$arrField.wrong name",
},
},
//group again as array field
{
$group: {
_id: "$_id",
arrField: { $push: "$arrField" },
},
},
//dump the output of pipeline into your collection, which will override it. Use with caution!
{
$out: "testcol",
},
]);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…