基础设施建设
mongodb连接器
private $db;
public function __construct(){
//enter code here`
try {
$this->db = new MongoDB\Driver\Manager('mongodb://192.168.0.117:27017');
}
catch (Exception $e) {
die('Erreur : '.$e->getmessage());
}
}
代码更新文档
public function mongoWrite($method, $records, $dbCollection, $filter = [], $options = ['upsert' => false]){
$this->__construct();
try {
$bulk = new MongoDB\Driver\BulkWrite();
if ($method == 'insert'){
foreach ($records as $record) {
$bulk->insert($record);
}
}
elseif ($method == 'update') {
foreach ($records as $record) {
$bulk->update($filter, ['$set' => $record], $options);
}
}
}
catch (MongoDB\Driver\Exception\Exception $e) {
echo $e->getMessage();
}
return $this->db->executeBulkWrite($dbCollection, $bulk);
}
我的基本文件如下
{
"_id" : ObjectId("5c067dbfefad3805b000306b"),
"qr_code" : "mVkcMV822UUH6OsdZclJaDe0/idEA==",
"approot" : "JAUGEAGE",
"NIU" : "494-417-972",
"proprietaire" : "nomo pierre",
"document" : "Photos camion",
"donnees" : [
{
"content" : "computer"
}
],
"statut" : "desactivé",
"date_creation" : "2018-12-04 13:14:39",
"date_modification" : ""
}
更新文档后
public function UpdateDataQr($qrcode, $args){
$array = [];
$array['donnees'][0]['content'] = 'blabla';
var_dump($this->mongoWrite('update', [$array], 'amla_sqr.sqrdata', ['qr_code' => $qrcode]));
}
我看到在donnee中没有新插入,但是内容被新值压碎了。
请帮助!