如何重命名 mongodb 中的字段(3 个示例)


您可以使用以下方法来重命名 MongoDB 中的字段:

方法一:重命名字段

 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:重命名字段

我们可以使用以下代码将team字段重命名为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' }

请注意,每个文档的team字段已重命名为new_team

示例 2:重命名多个字段

我们可以使用以下代码将team字段重命名为new_team ,将Points字段重命名为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 }

请注意,字段中的div子字段已在每个文档中重命名为“division”

注意:您可以在此处找到$rename函数的完整文档。

其他资源

以下教程解释了如何在 MongoDB 中执行其他常见操作:

MongoDB:如何添加新字段
MongoDB:如何删除字段
MongoDB:如何计算字段中的不同值

添加评论

您的电子邮箱地址不会被公开。 必填项已用*标注