Discussion:
[mongodb-user] How to rename fields in array?
Дима Прусский
2018-04-18 22:48:06 UTC
Permalink
I have following structure of documents:

{
"_id" : 12421,
"first" : {
"second" : {
"someFields" : 31241,
"third" : [
{
"wrongFieldName" : {
"something" : 2134214
}
},
{
"wrongFieldName" : {
"something" : 23413
}
}
]
}
}}

I want to change all "wrongFieldName" to "rightFieldName":

{
"_id" : 12421,
"first" : {
"second" : {
"someFields" : 31241,
"third" : [
{
"rightFieldName" : {
"something" : 2134214
}
},
{
"rightFieldName" : {
"something" : 23413
}
}
]
}
}}

I tried to use $rename, but it was thrown an error, because needed fields are in array:

The source field for $rename may not be dynamic: first.second.third.$.wrongFieldName
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/e7553ab1-2395-43d7-9a89-b77edbfcf288%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Chandraneel
2018-04-24 14:35:37 UTC
Permalink
Worked using forEach:

db.collection.find({}).forEach(function(doc){
for(var i=0;i<doc.first.second.third.length;i++){

doc.first.second.third[i].correctFieldName=doc.first.second.third[i].wrongFieldName;
delete doc.first.second.third[i].wrongFieldName;
}
db.collection.save(doc);
});
Post by Дима Прусский
{
"_id" : 12421,
"first" : {
"second" : {
"someFields" : 31241,
"third" : [
{
"wrongFieldName" : {
"something" : 2134214
}
},
{
"wrongFieldName" : {
"something" : 23413
}
}
]
}
}}
{
"_id" : 12421,
"first" : {
"second" : {
"someFields" : 31241,
"third" : [
{
"rightFieldName" : {
"something" : 2134214
}
},
{
"rightFieldName" : {
"something" : 23413
}
}
]
}
}}
The source field for $rename may not be dynamic: first.second.third.$.wrongFieldName
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/49d65deb-e23c-4295-86ea-31231da90805%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...