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
)
推荐阅读
- PMSJ寻平面设计师之现代(Hyundai)
- 太平之莲
- 闲杂“细雨”
- 七年之痒之后
- 深入理解Go之generate
- 由浅入深理解AOP
- 期刊|期刊 | 国内核心期刊之(北大核心)
- 生活随笔|好天气下的意外之喜
- 感恩之旅第75天
- MongoDB,Wondows下免安装版|MongoDB,Wondows下免安装版 (简化版操作)