"Copy" Data from one attribute to another for all documents in collection [mongoDB

49 views Asked by At

I have a DB which inherits some thousands of documents. In these documents are translations. They look like this:

{
 "id" : "1234",
 "translations" : {
    "en": "Mrs.",
    "it": "Signora",
    "de": "Frau"
 }
}

Now I want to "copy" the "de" translation and set a new attribute with the values of "de".

It should look like this:

{
 "id" : "1234",
 "translations" : {
    "en": "Mrs.",
    "it": "Signora",
    "de": "Frau",
    "de_formal": "Frau" //= "de".value
 }
}

I'm not quite sure how to achieve this. My attempt was something like this:

db.translations_test.updateMany({}, {$set : {"translations.de_formal" : db.translations_test.find({}, { "translations.de" : ""})}}, false, true);

1

There are 1 answers

2
jQueeny On BEST ANSWER

You can access the translations.de value in your updateMany by using the $ as follows:

db.translations_test.updateMany({}, [ {$set: {"translations.de_formal": "$translations.de"} } ]);