-1

I need equivalent mongo query to below SQL query:

Update tableName set tableName.col1 = tableName.col2
sidgate
  • 13,104
  • 9
  • 60
  • 107
Suresh Kamrushi
  • 14,655
  • 12
  • 74
  • 87

1 Answers1

1

Update: I had given incorrect solution earlier. As the answer is accepted, updating it with the right way

db.collectionName.find().snapshot().forEach(function (elem) {
        db.collectionName.update({_id: elem._id}, {$set: {col2: elem.col1}});
    }
);

Use normal update query along wiht $set to set new field value

db.collectionName.update({}, {$set: {col2: '$col1'}}, {multi: true})

sidgate
  • 13,104
  • 9
  • 60
  • 107
  • This will update the `col2` field in all the documents in the collection to have the string literal `'$col1'`, not the value from `col1`. – chridam Jan 09 '17 at 09:17