db.getCollection("posts")
.find({})
.forEach((item) => {
const newValues = item.resources
? item.resources.map((label) => ({ label }))
: [];
item.resources = newValues;
db.posts.save(item);
});
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’}] }
Top comments (0)