I have a collection in MongoDb someCollection
containing field field1
.
I want to add another field with values equal to field1
. I do it like this:
db.someCollection.updateMany({}, {$set:{"newField": "$field1"}})
But the after update I got row like this
{"field1": "value1", "newField": "$field1"}
But expected result is:
{"field1": "value1", "newField": "value1"}
What is wrong?
2
Answers
You can use update with pipeline. It looks almost the same, but the second part uses
[]
:See how it works on the playground example
Try this one: