Search code examples
stringmongodbprepend

prepend string to entire column in mongodb


I would like to prepend a string to all values of a mongo collection's column.

Something like

 db.testcoll.update({},{$set:{column1 : "prependstring"+ column1}});

Is there something like this ?


Solution

  • That can be achieved using the $concat operator in an aggregation pipeline.

    db.testcoll.aggregate([{
      $project: {
        column1: {
          $concat: ['prependstring', '$column1']
        }
      }
    }]);
    

    As specified in the official MongoDB docs (here) the $concat operator only works with strings.