PHP7之mongodb增删改查操作

实现参考自菜鸟教程

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php


namespace Admin\Util;

use MongoDB\Driver\Manager;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\Query;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Command;
class MongoCrud{
public static $mongo = null;

/**
* 新增数据到mongo
* @param $collection 表名
* @param $document 新增的数据
* @return \MongoDB\Driver\WriteResult
*/
public static function insertToMongo($collection, $document){
if (self::$mongo == null){
self::$mongo = new Manager("mongodb://user:password@127.0.0.1:27017/test");
}
$bulk = new BulkWrite;
$bulk->insert($document);
$writeConcern = new WriteConcern(WriteConcern::MAJORITY, 1000);
$result = self::$mongo->executeBulkWrite("test.{$collection}", $bulk, $writeConcern);
return $result;
}

/**
* 根据id删除mongodb中的数据
* @param $collection 表名
* @param $id 需要删除的id
* @return \MongoDB\Driver\WriteResult
*/
public static function deleteToMongoById($collection, $id){
if (self::$mongo == null)
self::$mongo = new Manager("mongodb://user:password@127.0.0.1:27017/test");
$bulk = new BulkWrite;
$bulk->delete(['id' => $id]);
$writeConcern = new WriteConcern(WriteConcern::MAJORITY, 1000);
$result = self::$mongo->executeBulkWrite("test.{$collection}", $bulk, $writeConcern);
return $result;
}

/**
* 查询数据
* @param $collection 表名
* @param $filter 过滤条件
* @param $options 排序,分页等参数
* @return array
* @throws \MongoDB\Driver\Exception\Exception
*/
public static function queryToMongo($collection, $filter, $options){
if (self::$mongo == null)
self::$mongo = new Manager("mongodb://user:password@127.0.0.1:27017/test");
$query = new Query($filter, $options);
$cursor = self::$mongo->executeQuery("test.{$collection}", $query);
$cursor = $cursor->toArray();
$count = count($cursor);
for ($i = 0; $i < $count; $i++){
$cursor[$i] = json_decode(json_encode($cursor[$i]), true);
}
return $cursor;
}

/**
* 获取表中数据的数量
* @param $collection 表名
* @param array $where 过滤条件
* @return int 数量
* @throws \MongoDB\Driver\Exception\Exception
*/
public static function count($collection, $where = []){
if (self::$mongo == null)
self::$mongo = new Manager("mongodb://user:password@127.0.0.1:27017/test");
$command = new Command(['count' => $collection,'query'=>$where]);

$result = self::$mongo->executeCommand('test',$command);
$res = $result->toArray();
$cnt = 0;
if ($res) {
$cnt = $res[0]->n;
}
//setAdminLog("表中的数目:{$cnt}");
return $cnt;
}

/**
* 根据id来更新mongodb的数据
* @param $collection 表名
* @param $document 需要更新的文档
* @param $id
* @return \MongoDB\Driver\WriteResult
*/
public static function updateDocumentById($collection, $document, $id){
if (self::$mongo == null)
self::$mongo = new Manager("mongodb://user:password@127.0.0.1:27017/test");

$bulk = new BulkWrite;
$bulk->update(['id' => $id],
['$set' => $document],
['multi' => false, 'upsert' => false]
);
$writeConcern = new WriteConcern(WriteConcern::MAJORITY, 1000);
$result = self::$mongo->executeBulkWrite("test.{$collection}", $bulk, $writeConcern);
return $result;
}

}
-------------本文结束您的阅读与肯定是我持续装*的最大动力-------------