全站年SVIP
全站1000+试题无限查看
单条插入:
$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]); // limit 为 1 时,删除第一条匹配数据 $bulk->delete(['id' => 2], ['limit' => 0]); // 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);
php操作MongoDB(CURD)
单条插入:
批量插入:
查询:
更新数据
删除