我试图使用 php 和 MongoDB 将一些字符串附加到字段的值中,但未能这样做。我的代码如下:
$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";
}
使用此代码,我可以更新除 feedStatusDescription 之外的所有其他字段,知道缺少什么吗?
谢谢
$concat是一个聚合运算符。
$set期望<field> => <value>
对,而不是<field> => <expression>
.
Mongodb 不支持引用其他字段的计算值。请参阅 Jira 中的功能请求。
您需要获取文档,更新其字段并保留它。