mongodb 对文档字段的重命名 $rename
发布日期:2021-11-15 14:57:49 浏览次数:28 分类:技术文章

本文共 1209 字,大约阅读时间需要 4 分钟。

$rename

可用于对文档字段的重命名。

使用方式:

{$rename: { 
:
,
:
, ... } }

使用实例:

students集合的文档:

{  "_id": 1,  "alias": [ "The American Cincinnatus", "The American Fabius" ],  "mobile": "555-555-5555",  "name": { "first" : "george", "last" : "washington" },  "friend": [		{"first" : "zhang", "last" : "san"},		{"first" : "li", "last" : "si"}	]}{  "_id": 2,  "alias": [ "My dearest friend" ],  "mobile": "222-222-2222",  "name": { "first" : "abigail", "last" : "adams" },  "friend": [		{"first" : "zhang", "last" : "san"},		{"first" : "li", "last" : "si"}	]}{  "_id": 3,  "alias": [ "Amazing grace" ],  "mobile": "111-111-1111",  "name": { "first" : "grace", "last" : "hopper" },  "friend": [		{"first" : "zhang", "last" : "san"},		{"first" : "li", "last" : "si"}	]}

对students集合所有文档的nam字段更改为name,name.first字段更改为name.firstName,如下

db.getCollection('students').updateMany({},{$rename:{    "nam":"name",    "name.first":"name.firstName"}});
注!!!

$rename对于数组内对象的key值暂无法更改

例如:friend数组内的first字段无法通过$rename变更。

变更数组内对象的key值

但数组内对象的key值可以通过遍历进行修改

例如下面的语句:

db.getCollection('students').find({}).forEach( function(item){		for(let i = 0; i

这样就可以将friend数组内的first字段变更为firstName字段。

!!!!!!

转载地址:https://blog.csdn.net/qq_39741836/article/details/115526999 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:MacBook Pro接通电源后电池不充电
下一篇:mongodb-config集合-system.sessions

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年03月29日 03时13分32秒