MongoDB: Set the value of array element from values another array

I want to create a new array methods[].linkToRegistry[] from methods[].settings[].linkToRegistry, i.e. I go through all the settings[] elements and extract data from the linkToRegistry fields into a new array methods[].linkToRegistry[] . The only condition is to skip empty linkToRegistry[] elements (optional, but desirable). Accordingly, for each methods[] element, the linkToRegistry[] array must be different. But I can only do this:
Source collection:

[
  {
    "methods": [
      {
        "settings": [
          {
            "linkToRegistry": "yes"
          }
        ],
      },
      {
        "settings": [
          {
            "linkToRegistry": "true"
          },
          {
            "linkToRegistry": "false"
          }
        ],
      },
      {
        "settings": [
          {
            "someField": "yes"
          }
        ],
      },
    ],
  },
  {
    "methods": [
      {
        "settings": [
          {
            "linkToRegistry": "NO"
          }
        ],
      },
      {
        "settings": [
          {
            "linkToRegistry": ""
          }
        ],
        
      }
    ],
  }
]

The code:

db.collection.update({
  "methods.settings.linkToRegistry": {
    $exists: true
  }
},
[
  {
    "$set": {
      "methods.linkToRegistry": "$methods.settings.linkToRegistry"
    }
  }
],
{
  multi: true
})

Final collection:

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "methods": [
      {
        "linkToRegistry": [
          [
            "yes"
          ],
          [
            "true",
            "false"
          ],
          []
        ],
        "settings": [
          {
            "linkToRegistry": "yes"
          }
        ]
      },
      {
        "linkToRegistry": [
          [
            "yes"
          ],
          [
            "true",
            "false"
          ],
          []
        ],
        "settings": [
          {
            "linkToRegistry": "true"
          },
          {
            "linkToRegistry": "false"
          }
        ]
      },
      {
        "linkToRegistry": [
          [
            "yes"
          ],
          [
            "true",
            "false"
          ],
          []
        ],
        "settings": [
          {
            "someField": "yes"
          }
        ]
      }
    ]
  },
  {
    "_id": ObjectId("5a934e000102030405000001"),
    "methods": [
      {
        "linkToRegistry": [
          [
            "NO"
          ],
          [
            ""
          ]
        ],
        "settings": [
          {
            "linkToRegistry": "NO"
          }
        ]
      },
      {
        "linkToRegistry": [
          [
            "NO"
          ],
          [
            ""
          ]
        ],
        "settings": [
          {
            "linkToRegistry": ""
          }
        ]
      }
    ]
  }
]

What I want to see:

[
  {
    "methods": [
      {
        "linkToRegistry": [
          [
            "yes"
          ]
        ],
        "settings": [
          {
            "linkToRegistry": "yes"
          }
        ]
      },
      {
        "linkToRegistry": [
          [
            "true",
            "false"
          ]
        ],
        "settings": [
          {
            "linkToRegistry": "true"
          },
          {
            "linkToRegistry": "false"
          }
        ]
      },
      {
        "linkToRegistry": [],
        "settings": [
          {
            "someField": "yes"
          }
        ]
      }
    ]
  },
  {
    "methods": [
      {
        "linkToRegistry": [
          [
            "NO"
          ]
        ],
        "settings": [
          {
            "linkToRegistry": "NO"
          }
        ]
      },
      {
        "linkToRegistry": [
          [
            ""
          ]
        ],
        "settings": [
          {
            "linkToRegistry": ""
          }
        ]
      }
    ]
  }
]

I’m complite:

db.getSiblingDB("ervk_core").getCollection("supervision").find({}).forEach(function (supervision) {
    if (supervision.methods instanceof Array) {
        supervision.methods.forEach(function (method) {
            var linkToRegistry = []
            var index = supervision.methods.indexOf(method)
            if (method.settings instanceof Array) {

                method.settings.forEach(function (setting) {
                    linkToRegistry.push(setting.linkToRegistry);
                });

                db.getSiblingDB("ervk_core").getCollection("supervision").updateOne(
                    {
                      _id: supervision._id,
                      methods: method
                    },
                    {
                      '$set':
                      {
                         [`methods.$.linkToRegistry`] : linkToRegistry
                      }
                    }
                );
            }
        });
    }
});

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.