Mongodb でフィールドの名前を変更する方法 (3 つの例)
次のメソッドを使用して、MongoDB のフィールドの名前を変更できます。
方法 1: フィールドの名前を変更する
db.collection.updateMany({}, { $rename :{" oldField ":" newField "}}, false, true)
方法 2: 複数のフィールドの名前を変更する
db.collection.updateMany({}, { $rename :{" old1 ":" new1 ", " old2 ":" new2 "}}, false, true)
方法 3: サブフィールドの名前を変更する
db.collection.updateMany({}, { $rename :{" field.oldSub ":" field.newSub "}}, false, true)
$rename 関数のfalse、true は{upsert:false, multi:true}を意味することに注意してください。
すべてのドキュメントのフィールド名を更新するには、 multi:trueが必要です。
次の例は、次の文書を使用して収集チームで各方法を使用する方法を示しています。
db.teams.insertOne({team: "Mavs", class: {conf: "Western", div: "A"}, points: 31 })
db.teams.insertOne({team: "Spurs", class: {conf: "Western", div: "A"}, points: 22 })
db.teams.insertOne({team: "Jazz", class: {conf: "Western", div: "B"}, points: 19 })
db.teams.insertOne({team: "Celtics", class: {conf: "Eastern", div: "C"}, points: 26 })
db.teams.insertOne({team: "Cavs", class: {conf: "Eastern", div: "D"}, points: 33 })
db.teams.insertOne({team: "Nets", class: {conf: "Eastern", div: "D"}, points: 38 })
例 1: フィールドの名前を変更する
次のコードを使用して、チームフィールドの名前をnew_teamに変更できます。
db.teams.updateMany({}, { $rename :{" team ":" new_team "}}, false, true)
現在のドキュメントは次のようになります。
{ _id: ObjectId("62017ce6fd435937399d6b58"),
class: { conf: 'Western', div: 'A' },
points: 31,
new_team: 'Mavs' }
{ _id: ObjectId("62017ce6fd435937399d6b59"),
class: { conf: 'Western', div: 'A' },
points: 22,
new_team: 'Spurs' }
{ _id: ObjectId("62017ce6fd435937399d6b5a"),
class: { conf: 'Western', div: 'B' },
points: 19,
new_team: 'Jazz' }
{ _id: ObjectId("62017ce6fd435937399d6b5b"),
class: { conf: 'Eastern', div: 'C' },
points: 26,
new_team: 'Celtics' }
{ _id: ObjectId("62017ce6fd435937399d6b5c"),
class: { conf: 'Eastern', div: 'D' },
points: 33,
new_team: 'Cavs' }
{ _id: ObjectId("62017ce6fd435937399d6b5d"),
class: { conf: 'Eastern', div: 'D' },
points: 38,
new_team: 'Nets' }
各ドキュメントのチームフィールドの名前がnew_teamに変更されていることに注意してください。
例 2: 複数のフィールドの名前を変更する
次のコードを使用して、チームフィールドの名前をnew_teamに変更し、ポイントフィールドの名前をnew_pointsに変更できます。
db.teams.updateMany({}, { $rename :{" team ":" new_team ", " points ":" new_points "}}, false, true)
現在のドキュメントは次のようになります。
{ _id: ObjectId("62017ce6fd435937399d6b58"),
class: { conf: 'Western', div: 'A' },
new_team: 'Mavs',
new_points: 31 }
{ _id: ObjectId("62017ce6fd435937399d6b59"),
class: { conf: 'Western', div: 'A' },
new_team: 'Spurs',
new_points: 22 }
{ _id: ObjectId("62017ce6fd435937399d6b5a"),
class: { conf: 'Western', div: 'B' },
new_team: 'Jazz',
new_points: 19 }
{ _id: ObjectId("62017ce6fd435937399d6b5b"),
class: { conf: 'Eastern', div: 'C' },
new_team: 'Celtics',
new_points: 26 }
{ _id: ObjectId("62017ce6fd435937399d6b5c"),
class: { conf: 'Eastern', div: 'D' },
new_team: 'Cavs',
new_points: 33 }
{ _id: ObjectId("62017ce6fd435937399d6b5d"),
class: { conf: 'Eastern', div: 'D' },
new_team: 'Nets',
new_points: 38 }
各ドキュメントでは、チームフィールドとポイントフィールドの両方の名前が変更されていることに注意してください。
例 3: サブフィールドの名前を変更する
次のコードを使用して、 divクラスフィールドのdivサブフィールドの名前を変更できます。
db.teams.updateMany({}, { $rename :{" class.div ":" class.division "}}, false, true)
現在のドキュメントは次のようになります。
{ _id: ObjectId("62017e21fd435937399d6b5e"),
team: 'Mavs',
class: { conf: 'Western', division: 'A' },
points: 31 }
{ _id: ObjectId("62017e21fd435937399d6b5f"),
team: 'Spurs',
class: { conf: 'Western', division: 'A' },
points: 22 }
{ _id: ObjectId("62017e21fd435937399d6b60"),
team: 'Jazz',
class: { conf: 'Western', division: 'B' },
points: 19 }
{ _id: ObjectId("62017e21fd435937399d6b61"),
team: 'Celtics',
class: { conf: 'Eastern', division: 'C' },
points: 26 }
{ _id: ObjectId("62017e21fd435937399d6b62"),
team: 'Cavs',
class: { conf: 'Eastern', division: 'D' },
points: 33 }
{ _id: ObjectId("62017e21fd435937399d6b63"),
team: 'Nets',
class: { conf: 'Eastern', division: 'D' },
points: 38 }
各ドキュメントでは、 classフィールドのdivサブフィールドの名前がDivisionに変更されていることに注意してください。
注: $rename関数の完全なドキュメントはここで見つけることができます。
追加リソース
次のチュートリアルでは、MongoDB で他の一般的な操作を実行する方法について説明します。
MongoDB: 新しいフィールドを追加する方法
MongoDB: フィールドを削除する方法
MongoDB: フィールド内の個別の値をカウントする方法