-1

I need equivalent mongo query to below SQL query:

Update tableName set tableName.col1 = tableName.col2
sidgate
  • 14,650
  • 11
  • 68
  • 119
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90

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
  • 14,650
  • 11
  • 68
  • 119
  • 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