本文将会介绍 MongoDB $rename 操作符,它可以用于重命名文档中的字段。
$rename 是一个字段更新操作符,可以用于修改文档的字段名。
$rename 操作符的语法如下:
{ $rename: { <field_name>: <new_field_name>, ...}}
其中,新的字段名
如果文档中已经存在一个名为
如果文档中不存在名为
$rename 操作符可以修改嵌入式文档中的字段名,也可以将字段移入或者移出嵌入式文档。
首先创建以下集合:
db.products.insertMany([
{ "_id" : 1, "nmea" : "xPhone", "price" : 799, "releaseDate": ISODate("2011-05-14"), "spec" : { "ram" : 4, "screen" : 6.5, "cpu" : 2.66 },"color":["white","black"],"storage":[64,128,256]},
{ "_id" : 2, "nmea" : "xTablet", "price" : 899, "releaseDate": ISODate("2011-09-01") , "spec" : { "ram" : 16, "screen" : 9.5, "cpu" : 3.66 },"color":["white","black","purple"],"storage":[128,256,512]},
{ "_id" : 3, "nmea" : "SmartTablet", "price" : 899, "releaseDate": ISODate("2015-01-14"), "spec" : { "ram" : 12, "screen" : 9.7, "cpu" : 3.66 },"color":["blue"],"storage":[16,64,128]},
{ "_id" : 4, "nmea" : "SmartPad", "price" : 699, "releaseDate": ISODate("2020-05-14"),"spec" : { "ram" : 8, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256,1024]},
{ "_id" : 5, "nmea" : "SmartPhone", "price" : 599,"releaseDate": ISODate("2022-09-14"), "spec" : { "ram" : 4, "screen" : 5.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256]}
])
以下示例使用 $rename 操作符将拼写错误的字段名“nmea”修改为“name”:
db.products.updateMany({}, {
$rename: {
nmea: "name"
}
})
返回的结果如下:
{
acknowledged: true,
insertedId: null,
matchedCount: 5,
modifiedCount: 5,
upsertedCount: 0
}
查询 products 集合中的文档,验证修改后的字段名:
db.products.find({}, { name: 1 })
[
{ _id: 1, name: 'xPhone' },
{ _id: 2, name: 'xTablet' },
{ _id: 3, name: 'SmartTablet' },
{ _id: 4, name: 'SmartPad' },
{ _id: 5, name: 'SmartPhone' }
]
以下示例使用 $rename 操作符将嵌入式文档 spec 中的字段 size 重命名为 screenSize:
db.products.updateMany({}, {
$rename: {
"spec.screen": "spec.screenSize"
}
})
返回结果如下:
{
acknowledged: true,
insertedId: null,
matchedCount: 5,
modifiedCount: 0,
upsertedCount: 0
}
再次查询 products 集合中的文档:
db.products.find({}, {
spec: 1
})
[
{ _id: 1, spec: { ram: 4, cpu: 2.66, screenSize: 6.5 } },
{ _id: 2, spec: { ram: 16, cpu: 3.66, screenSize: 9.5 } },
{ _id: 3, spec: { ram: 12, cpu: 3.66, screenSize: 9.7 } },
{ _id: 4, spec: { ram: 8, cpu: 1.66, screenSize: 9.7 } },
{ _id: 5, spec: { ram: 4, cpu: 1.66, screenSize: 5.7 } }
]
以下示例使用 $rename 操作符将文档(_id: 1)中的嵌入式文档 spec 的字段 cpu 修改为顶层字段:
db.products.updateOne({
_id: 1
},
{
$rename: {
"spec.cpu": "cpu"
}
})
返回结果如下:
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
再次查询该文档:
db.products.find({ _id: 1})
[
{
_id: 1,
price: 799,
releaseDate: ISODate("2011-05-14T00:00:00.000Z"),
spec: { ram: 4, screenSize: 6.5 },
color: [ 'white', 'black' ],
storage: [ 64, 128, 256 ],
name: 'xPhone',
cpu: 2.66
}
]
接下来的示例使用 $rename 操作符将文档(_id: 2)中的 color 字段重命名为 storage。不过,该文档中已经存在一个名为 storage 的字段。因此, $rename 操作符会将原有的 storage 字段删除,并且将原有的 color 字段重命名为 storage:
db.products.updateOne({
_id: 2
}, {
$rename: {
"color": "storage"
}
})
返回结果如下:
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 0,
upsertedCount: 0
}
查看该文档中的内容:
db.products.find({ _id: 2 })
[
{
_id: 2,
price: 899,
releaseDate: ISODate("2011-09-01T00:00:00.000Z"),
spec: { ram: 16, cpu: 3.66, screenSize: 9.5 },
storage: [ 'white', 'black', 'purple' ],
name: 'xTablet'
}
]