{"id":378,"title":"php\u64cd\u4f5cmongodb","good":0,"bad":0,"hit":2201,"created_at":"2018-07-31 00:15:47","content":"
<?php\n\n\/**\n * php\u64cd\u4f5cmongodb\n * 127.0.0.1\/index.php?action=\u65b9\u6cd5&where=\u7b49\u7b49\u53c2\u6570\u5c31\u4f1a\u8fd4\u56dejson\n *\/\nclass MongodbClient\n{\n\n protected $mongodb;\n protected $dbname;\n protected $collection;\n protected $bulk;\n protected $writeConcern;\n\n public function __construct($config)\n {\n if (!$config['dbname'] || !$config['collection']) {\n # code...\n exit('\u914d\u7f6e\u9519\u8bef');\n }\n $this->mongodb = new MongoDB\\Driver\\Manager("mongodb:\/\/localhost:27017");\n $this->dbname = $config['dbname'];\n $this->collection = $config['collection'];\n $this->bulk = new MongoDB\\Driver\\BulkWrite();\n $this->writeConcern = new MongoDB\\Driver\\WriteConcern(MongoDB\\Driver\\WriteConcern::MAJORITY, 100);\n }\n\n \/**\n * Created by PhpStorm.\n * function: query\n * Description:\u67e5\u8be2\u65b9\u6cd5\n * User: Xiaoxie\n * Email 736214763@qq.com\n * @param array $where\n * @param array $option\n * @return string\n *\n *\/\n public function query($where = [], $option = [])\n {\n $query = new MongoDB\\Driver\\Query($where, $option);\n $result = $this->mongodb->executeQuery("$this->dbname.$this->collection", $query);\n $data = [];\n if ($result) {\n # code...\n foreach ($result as $key => $value) {\n # code...\n array_push($data, $value);\n }\n }\n\n return json_encode($data);\n }\n\n \/**\n * Created by PhpStorm.\n * function: getCount\n * Description:\u83b7\u53d6\u7edf\u8ba1\u6570\n * User: Xiaoxie\n * Email 736214763@qq.com\n * @param array $where\n * @return int\n *\n *\/\n public function getCount($where = [])\n {\n $command = new MongoDB\\Driver\\Command(['count' => $this->collection, 'query' => $where]);\n $result = $this->mongodb->executeCommand($this->dbname, $command);\n $res = $result->toArray();\n $cnt = 0;\n if ($res) {\n # code...\n $cnt = $res[0]->n;\n }\n\n return $cnt;\n }\n\n \/**\n * Created by PhpStorm.\n * function: page\n * Description:\u5206\u9875\u6570\u636e\n * User: Xiaoxie\n * Email 736214763@qq.com\n * @param array $where\n * @param int $page\n * @param int $limit\n * @return string\n *\n *\/\n public function page($where = [], $page = 1, $limit = 10)\n {\n\n $count = $this->getCount($where);\n $data['count'] = $count;\n $endpage = ceil($count \/ $limit);\n if ($page > $endpage) {\n # code...\n $page = $endpage;\n } elseif ($page < 1) {\n $page = 1;\n }\n $skip = ($page - 1) * $limit;\n $options = [\n 'skip' => $skip,\n 'limit' => $limit\n ];\n $data['data'] = $this->query($where, $options);\n $data['page'] = $endpage;\n return json_encode($data);\n }\n\n \/**\n * Created by PhpStorm.\n * function: update\n * Description:\u66f4\u65b0\u64cd\u4f5c\n * User: Xiaoxie\n * Email 736214763@qq.com\n * @param array $where\n * @param array $update\n * @param bool $upsert\n * @return int|null\n *\n *\/\n public function update($where = [], $update = [], $upsert = false)\n {\n $this->bulk->update($where, ['$set' => $update], ['multi' => true, 'upsert' => $upsert]);\n $result = $this->mongodb->executeBulkWrite("$this->dbname.$this->collection", $this->bulk, $this->writeConcern);\n return $result->getModifiedCount();\n }\n\n \/**\n * Created by PhpStorm.\n * function: insert\n * Description:\u63d2\u5165\n * User: Xiaoxie\n * Email 736214763@qq.com\n * @param array $data\n * @return mixed\n *\n *\/\n public function insert($data = [])\n {\n $result = $this->bulk->insert($data);\n return $result->getInsertedCount();\n }\n\n \/**\n * Created by PhpStorm.\n * function: delete\n * Description:\u5220\u9664\n * User: Xiaoxie\n * Email 736214763@qq.com\n * @param array $where\n * @param int $limit\n * @return mixed\n *\n *\/\n public function delete($where = [], $limit = 1)\n {\n $result = $this->bulk->delete($where, ['limit' => $limit]);\n return $result->getDeletedCount();\n }\n\n}\n\/\/\u5b9e\u4f8b\u5316\u8c03\u7528\n$action = $_GET['action'] ?: exit('\u53c2\u6570\u9519\u8bef');\n$page = $_GET['page'] ?: 1;\n$where = json_decode($_GET['where'], true) ?: [];\n$limit = $_GET['limit'] ?: '10';\n$data = json_decode($_GET['data'], true) ?: [];\n$option = json_decode($_GET['option'], true) ?: [];\n$collection = $_GET['collection'];\/\/\u5e93\u540d.\u96c6\u5408\u540d\n$dbname = 'local';\n$mongodb = new MongodbClient(['dbname' => $dbname, 'collection' => $collection]);\n\nif ($action == 'getCount') {\n $data = $mongodb->getCount($where);\n} elseif ($action == 'insert') {\n $data = $mongodb->insert($data);\n} elseif ($action == 'update') {\n $data = $mongodb->update($where, $data);\n} elseif ($action == 'delete') {\n $data = $mongodb->delete($where);\n} elseif ($action == 'query') {\n $data = $mongodb->query($where, $option);\n} elseif ($action == 'page') {\n $data = $mongodb->page($where, $page, $limit);\n}\n\necho $data;<\/pre>
<\/p>"}