I have the following collection:
{
{
field1: 11,
field2: 22
},
{
field1: 111,
field2: 222
},
...
}
And I want to add a new field3, which uses field1 and field2 to populate his properties, name and code.
Something like this:
{
{
field1: 11,
field2: 22,
field3: {code: 11, name: 22}
},
{
field1: 111,
field2: 222,
field3: {code: 111, name: 222}
},
...
}
I have tried:
db.<collection>.update(
{
$set:{field3: {code: '$field1', name:'$field2'}}
},
{
multi:true
}
)
But got the string values '$field1' and '$field2':
{
{
field1: 11,
field2: 22,
field3: {code: '$field1', name: '$field2'}
},
{
field1: 111,
field2: 222,
field3: {code: '$field1', name: '$field2'}
},
...
}
I can try with aggregate and achieve the wanted result, but I think it can be done and with update.
Waiting for your suggestions, thanks.