insert(['x' => 1, 'name'=>'dadavm', 'url' => 'http://www.dadavm.cn']);$bulk->i。PHP7之Mongodb的开发详解。" />

PHP7之Mongodb的开发详解

1.mongdb的连接

new \MongoDB\Driver\Manager("mongodb://name:password@xxx.xxx.xx.xxx:27017/apps");

2.mongdb的添加
$bulk = new \MongoDB\Driver\BulkWrite; $bulk->insert(['x' => 1, 'name'=>'dadavm', 'url' => 'http://www.dadavm.cn']); $bulk->insert(['x' => 2, 'name'=>'Google', 'url' => 'http://www.google.com']); $bulk->insert(['x' => 3, 'name'=>'taobao', 'url' => 'http://www.taobao.com']); $db->executeBulkWrite('apps.test', $bulk);

3.mongdb的删除
$bulk = new \MongoDB\Driver\BulkWrite; $bulk->delete(['x' => 1], ['limit' => 1]); // limit 为 1 时,删除第一条匹配数据 $bulk->delete(['x' => 2], ['limit' => 0]); // limit 为 0 时,删除所有匹配数据 $writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000); $result = $db->executeBulkWrite('apps.test', $bulk, $writeConcern);

4.mongdb的修改
$bulk = new \MongoDB\Driver\BulkWrite; $bulk->update( ['x' => 2], ['$set' => ['name' => '', 'url' => 'jianshu.runoob.com']], ['multi' => false, 'upsert' => false] ); $writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000); $result = $db->executeBulkWrite('h5maker.test', $bulk, $writeConcern);

5.mongdb的查询
// 查询数据 $filter = ['x' => ['$gt' => 1]]; $options = [ 'projection' => ['_id' => 0], 'sort' => ['x' => -1], ]; $query = new \MongoDB\Driver\Query($filter, $options); $cursor = $db->executeQuery('apps.test', $query); foreach ($cursor as $document) { echo '
'; print_r($document); } exit;

【PHP7之Mongodb的开发详解】数据:
stdClass Object ( [x] => 3 [name] => taobao [url] => http://www.taobao.com) stdClass Object( [x] => 2 [name] => [url] => jianshu.runoob.com )

    推荐阅读