I am using MongoDB 6.0 and mongosh 1.5. I have a collection, which have elements with following structure:
{
"name": "Optimization using NN",
"calculations": {
"1": {
"code": "A123",
"purpose": "TIME",
"cities": {
"PARIS": {
"load": 50,
"price": 10.45
},
"ROME": {
"load": 100,
"price": 8.25
},
"LYON": {
"load": 40,
"price": 9.00
}
}
},
"2": {
"code": "B123",
"purpose": "COST",
"cities": {
"PARIS": {
"load": 150,
"price": 9.45
},
"ROME": {
"load": 40,
"price": 7.25
},
"LYON": {
"load": 30,
"price": 8.00
}
}
}
}
}
I need to add field to every calculation, which is not really dificult:
db.experiment.updateMany({},
[{
$set: {
'calculations': {
$objectToArray: '$calculations'
}
}
}, {
$addFields: {
'calculations.v.active': {
$literal: true
}
}
}, {
$set: {
'calculations': {
$arrayToObject: '$calculations'
}
}
}
])
Is there some way to add field "constraints" with value of empty object to all cities?
{
"name": "Optimization using NN",
"calculations": {
"1": {
"code": "A123",
"purpose": "TIME",
"cities": {
"PARIS": {
"load": 50,
"price": 10.45,
"constraints" : {}
},
"ROME": {
"load": 100,
"price": 8.25,
"constraints" : {}
},
"LYON": {
"load": 40,
"price": 9.00,
"constraints" : {}
}
},
"active" : true
},
"2": {
"code": "B123",
"purpose": "COST",
"cities": {
"PARIS": {
"load": 150,
"price": 9.45,
"constraints" : {}
},
"ROME": {
"load": 40,
"price": 7.25,
"constraints" : {}
},
"LYON": {
"load": 30,
"price": 8.00,
"constraints" : {}
}
},
"active" : true
}
}
}
I can't use $objectToArray again on '$calculations.v.cities', because it is already an array.
[{"index": 5 ...} ...]and avoiding to have data on schema. Else you can keep this schema but i think its not good idea