我必须更新mongodb集合中的记录, 所以使用终端,我可以这样做:
db.collection_name.update({"_id": ObjectId("562dbdc57d0175ee0f8b4569")}, {$set: {"status": "In-progress"}});
但我希望它能在我的PHP代码中完成。
那么如何使用PHP中的标准ObjectId更新记录?
public function updateStatus($status, $uuid) {
global $logger, $warnings_payload;
var_dump(array("_id" => $uuid));
var_dump(array("status" => $status));
$this-> mongo-> selectCollection('collection_name');
try {
$result = $this-> mongo -> update( array( '_id' => new MongoId($uuid)) , array($set => array("status" => $status)) );
}
catch (Exception $e){
var_dump($e);
}
return $result;
}
var_dump的结果(array(“_ id”=> $ uuid));
array(1) {
["_id"]=>
string(24) "562dbda57d01754e058b4567"
}
array(1) {
["status"]=>
string(9) "Delivered"
}
发生异常是:
object(MongoException)#44 (7) {
["message":protected]=>
string(67) "zero-length keys are not allowed, did you use $ with double quotes?"
["string":"Exception":private]=>
string(0) ""