0

I am trying to append some string to a field's value using php and MongoDB, but am failing to do so. My code is as under:

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$filter = ['_id' => new MongoDB\BSON\ObjectId($_POST['itemid'])];
$newObj = ['$set' => ["department" => $_POST['department'],
                      "subDepartment" => $_POST['subdepartment'],
                      "feedStatusDescription" => ['$concat' =>  ["$feedStatusDescription" , $_POST['description'] ]]  ,
                      "feedStatus" => $_POST['feedStatus']
                      ]
          ];
$options = ["multi" => true, "upsert" => false];
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update($filter, $newObj, $options);

try {
$result = $manager->executeBulkWrite("mydb.mycollection", $bulk, $wc);

} catch (MongoDB\Driver\Exception\Exception $e) {
    echo $e->getMessage(), "\n";
}

Using this code I am able to update all of other fields except feedStatusDescription, any idea what is missing?

Thanks

Satya
  • 8,693
  • 5
  • 34
  • 55
  • 1
    Possible duplicate of [Update MongoDB field using value of another field](http://stackoverflow.com/questions/3974985/update-mongodb-field-using-value-of-another-field) – Blakes Seven Mar 10 '16 at 11:24

1 Answers1

0

$concat is an aggregation operator.

$set expects <field> => <value> pairs, not <field> => <expression>.

Mongodb does not support computed values with references to other fields. See feature request in Jira.

You need to fetch a document, update its fields, and persist it.

Alex Blex
  • 34,704
  • 7
  • 48
  • 75