單條插入
$bulk = new MongoDB\Driver\BulkWrite;
$document = ['_id' => new MongoDB\BSON\ObjectID, 'name' => 'hello word'];
$bulk->insert($document);
$manager = new MongoDB\Driver\Manager("mongodb://192.168.110.30:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('my.hello', $bulk, $writeConcern);
批量插入
$manager = new MongoDB\Driver\Manager("mongodb://192.168.110.30:27017");
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['id' => 1, 'name'=>'php', 'url' => 'http://www.php.com']);
$bulk->insert(['id' => 2, 'name'=>'java', 'url' => 'http://www.java.com']);
$bulk->insert(['id' => 3, 'name'=>'css', 'url' => 'http://www.css.com']);
$result = $manager->executeBulkWrite('biancheng.cate', $bulk);
查詢
$filter=[
'id'=>['$gt'=>1]
];
$options=[
'projection'=>[
'_id'=>0
],
'sort'=>['x'=>-1]
];
$manager = new MongoDB\Driver\Manager("mongodb://192.168.110.30:27017");
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('biancheng.cate', $query);
foreach ($cursor as $document) {
echo "<pre>";
print_r($document);
}
更新資料
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
['id' => 2],
['$set' => ['name' => 'goLang', 'url' => 'tool.goLang.com']],
['multi' => false, 'upsert' => false]
);
$manager = new MongoDB\Driver\Manager("mongodb://192.168.110.30:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('biancheng.cate', $bulk, $writeConcern);
刪除
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->delete(['id' => 1], ['limit' => 1]);
$bulk->delete(['id' => 2], ['limit' => 0]);
$manager = new MongoDB\Driver\Manager("mongodb://192.168.110.30:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('biancheng.cate', $bulk, $writeConcern);
本作品採用《CC 協議》,轉載必須註明作者和本文連結