MongoDB concatenate strings from two fields into a third field

Unfortunately, MongoDB currently does not allow you to reference the existing value of any field when performing an update(). There is an existing Jira ticket to add this functionality: see SERVER-1765 for details.

At present, you must do an initial query in order to determine the existing values, and do the string manipulation in the client. I wish I had a better answer for you.


**

in my case this $concat worked for me ...

**

db.collection.update( { "_id" : {"$exists":true} }, 
   [ { 
       "$set" : { 
                 "column_2" :  { "$concat" : ["$column_4","$column_3"] }
                }
      }
   ]

You could use $set like this in 4.2 which supports aggregation pipeline in update.

db.collection.update(
   {"_id" :{"$exists":true}},
   [{"$set":{"column_2":{"$concat":["$column_4","$column_3"]}}}]
)

You can use aggregation operators $project and $concat:

db.collection.aggregate([
  { $project: { newfield: { $concat: [ "$field1", " - ", "$field2" ] } } }
])