P
P
PennyParker2022-03-11 23:06:36
MongoDB
PennyParker, 2022-03-11 23:06:36

Mongo - how to rename object keys?

I have a selection of objects

var pages = db.getCollection('blocks_copy')
.find({Type: 'page', Folder: {$in : ['bakeries/rosenborg/pages', 'bakeries/brogylen/pages']}, 'blocks': { $exists: true }}).toArray()
printjson(pages)
622ba8b280476162366442.png

For objects with this type, I'm interested in the blocks field and I need to rename all the keys that this field contains
pages.forEach(function(e){
    var changedNames = Object.keys(e.blocks).map(function(e){return `Rosenborg${e}`})
    printjson(changedNames)  
})


Okay, I have an array with new key values, but I don’t understand how to update the database in this case and what method to use for this.

Thanks for the help!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question