Updating a document based on another field

How can I use update() to update all documents by adding a new field based on value of an existing field in same document

I tried to use $ in value of $set but it didnt work

ex:

db.dummycoll.insertOne({f1:1,f2:2})

I want to update all documents to add new field f3 which should be the value of f2

db.dummycoll.update({},
{$set:{f3:’$f2’}})

above didnt work , it is setting string $f2

See the answer from this thread:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.