DEV Community


Mongo DB: Update data using `.forEach()`

Mehul Lakhanpal
Full stack Developer | Micro-blogging on Web Development at | Creator of File Ops, Note Box, GitHub Marker
Originally published at ・1 min read
  .forEach((item) => {
    const newValues = item.resources
      ? => ({ label }))
      : [];
    item.resources = newValues;;
Enter fullscreen mode Exit fullscreen mode

Task: Convert an array of string into array of object.
.forEach() can be used for some complex operations/migrations

Before: { _id: jf340f34f..., resources: [‘R1’] }

After: { _id: jf340f34f..., resources: [{label: ‘R1’}] }

Discussion (0)