0%

使用PHP对MongoDB进行CURD操作

MongoDB是一个基于分布式文件存储的数据库,由C++语言编写,旨在为WEB应用提供可扩展的高性能数据存储解决方案。MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富、最像关系数据库的。它支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是它支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。

一、原生操作

$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');

  1. 增加数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 方式一
$command3 = new MongoDB\Driver\Command([
'insert' => 'friend',
'documents' => [
['First Name' => "房", 'Last Name' => '瑞娜','Age' => 27],
['First Name' => "刘", 'Last Name' => '玉龙','Age' => 29],
]
]);
$cursor = $manager->executeCommand('test', $command3);
print_r($cursor->toArray());

// 方式二
$bulkWrite = new \MongoDB\Driver\BulkWrite(['ordered'=>true]);
$dbCollectionName = 'test.friend';
$data = [
'name' => '杨幂',
'actor' => '杨紫'
];
$bulkWrite->insert($data);
$cursor = $manager->executeBulkWrite($dbCollectionName,$bulkWrite);
print_r($cursor);exit;
  1. 删除数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 方式一
$command0 = new MongoDB\Driver\Command([
'delete' => 'friend',
'deletes' => [
[ 'q' => ['First Name' => '刘'],
'limit' => 0, // 0则删除全部,1则只删除一条
]
]
]);
$cursor = $manager->executeCommand('test', $command0);
print_r($cursor->toArray());

// 方式二
$bulkWrite = new \MongoDB\Driver\BulkWrite(['ordered'=>true]);
$writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY,1000);
$dbCollectionName = 'test.friend';
$extra = array('limit'=>1);
$where = ['name' => '杨幂'];
$bulkWrite->delete($where,$extra);
$cursor = $manager->executeBulkWrite($dbCollectionName,$bulkWrite,$writeConcern);
print_r($cursor);exit;
  1. 修改数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 方式一
$cmd = [
'update' => 'friend', // collection 表名
'updates' => [
[
'q' => ['Age' => 29], //更新条件array
'u' => ['$set' => ['name' => '修改']], //要更新的内容(这个等同于update()函数的更新的内容)
'upsert' => false, // 没有找到则插入,找到了则更新
'multi' => true // true符合条件的则全部更新,默认false只更新第一条
],
],
'ordered' => false, // 是否依次执行updates语句,true表示执行失败后继续后面的语句,false 表示一旦失败立即返回
];
$command6 = new MongoDB\Driver\Command($cmd);
$cursor = $manager->executeCommand('test', $command6);
print_r($cursor->toArray());

// 方式二
$bulkWrite = new \MongoDB\Driver\BulkWrite(['ordered'=>true]);
$writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY,1000);
$dbCollectionName = 'test.friend';
$where = ['name' => '杨幂'];
$update = ['name' => '大咪咪', 'actor' => '小雪'];
$extra = ['multi' => false, 'upsert'=>false];
$bulkWrite->update(
$where,
$update,
$extra
);
$cursor = $manager->executeBulkWrite($dbCollectionName,$bulkWrite,$writeConcern);
print_r($cursor);
  1. 查询数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 方式一
$command1 = new MongoDB\Driver\Command([//查出某一个
"find" => "friend",
"filter" => ["name" => "羽博"]
]);
$cursor = $manager->executeCommand('test', $command1);
print_r($cursor->toArray());

// 方式二
$command2 = new MongoDB\Driver\Command([
"distinct" => "friend",
"key" => "Age",
"query" => ["Age" => 27]
]);
$cursor = $manager->executeCommand('test', $command2);
print_r($cursor->toArray());exit;
  1. 统计
1
2
3
4
5
6
7
8
$where = ['name'  => '修改'];
$cmd = array(
'count' => 'friend',
'query' => $where
);
$command = new \MongoDB\Driver\Command($cmd);
$cursor = $manager->executeCommand('test',$command);
print_r($cursor->toArray());
  1. 索引
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 添加索引
$command5 = new MongoDB\Driver\Command([
'createIndexes' => 'friend',
'indexes' => [
[
'name' => 'email',
'key' => array('E-Mail' => 1),//1升序0降序
'unique' => false,
]
],
]);
$cursor = $manager->executeCommand('test', $command5);
print_r($cursor->toArray());

// 删除索引
$command6 = new MongoDB\Driver\Command([
'dropIndexes' => 'friend',
'index' => 'email',
]);
$cursor = $manager->executeCommand('test', $command6);
print_r($cursor->toArray());

二、基于Composer包mongodb/mongodb操作

  1. 安装依赖包 composer require mongodb/mongodb