~~~  ~~~ /\*\* \* mongoDB 简单 封装 \* 注意:支持 mongoDB 3.2+ \* 具体参数及相关定义请参见:[https://docs.mongodb.com/manual/reference/command/](https://docs.mongodb.com/manual/reference/command/) \* \* @author color\_wind \*/ finalclassm\_mgdb { privatestatic$ins= \[\]; privatestatic$def="default"; private$\_conn= null; private$\_db= null; privatestatic$\_config= \[ "default"=> \["url"=>"[mongodb://localhost:27017](mongodb://localhost:27017)","dbname"=>"mydb1"\], "mdb1"=> \["url"=>"[mongodb://10.0.0.12:27017](mongodb://10.0.0.12:27017)","dbname"=>"mydb2"\], \]; /\*\* \* 创建mongo对象 \* @param  string $confkey \* @return \\m\_mgdb \*/ staticfunctioni($confkey= NULL) { if(!$confkey) { $confkey= self::$def; } if(!isset(self::$ins\[$confkey\]) && ($conf= self::$\_config\[$confkey\])) { $m=newm\_mgdb($conf); self::$ins\[$confkey\] =$m; } returnself::$ins\[$confkey\]; } privatefunction\_\_construct(array$conf) { $this->\_conn =newMongoDB\\Driver\\Manager($conf\["url"\]); $this->\_db   =$conf\["dbname"\]; } /\*\* \* 插入数据 \* @param  string $collname \* @param  array  $documents    \[\["name"=>"values", ...\], ...\] \* @param  array  $writeOps     \["ordered"=>boolean,"writeConcern"=>array\] \* @return \\MongoDB\\Driver\\Cursor \*/ functioninsert($collname,array$documents,array$writeOps= \[\]) { $cmd= \[ "insert"=>$collname, "documents"=>$documents, \]; $cmd+=$writeOps; return$this->command($cmd); } /\*\* \* 删除数据 \* @param  string $collname \* @param  array  $deletes      \[\["q"=>query,"limit"=>int\], ...\] \* @param  array  $writeOps     \["ordered"=>boolean,"writeConcern"=>array\] \* @return \\MongoDB\\Driver\\Cursor \*/ functiondel($collname,array$deletes,array$writeOps= \[\]) { foreach($deletesas&$\_){ if(isset($\_\["q"\]) && !$\_\["q"\]){ $\_\["q"\] = (Object)\[\]; } if(isset($\_\["limit"\]) && !$\_\["limit"\]){ $\_\["limit"\] = 0; } } $cmd= \[ "delete"=>$collname, "deletes"=>$deletes, \]; $cmd+=$writeOps; return$this->command($cmd); } /\*\* \* 更新数据 \* @param  string $collname \* @param  array  $updates      \[\["q"=>query,"u"=>update,"upsert"=>boolean,"multi"=>boolean\], ...\] \* @param  array  $writeOps     \["ordered"=>boolean,"writeConcern"=>array\] \* @return \\MongoDB\\Driver\\Cursor \*/ functionupdate($collname,array$updates,array$writeOps= \[\]) { $cmd= \[ "update"=>$collname, "updates"=>$updates, \]; $cmd+=$writeOps; return$this->command($cmd); } /\*\* \* 查询 \* @param  string $collname \* @param  array  $filter     \[query\]     参数详情请参见文档 \* @param  array  $writeOps   \["key"=>vals,...\] 其它参数 参数详情请参见文档 \* @return \\MongoDB\\Driver\\Cursor \*/ functionquery($collname,array$filter,array$writeOps= \[\]){ $cmd= \[ "find"=>$collname, "filter"=>$filter \]; $cmd+=$writeOps; return$this->command($cmd); } /\*\* \* 执行MongoDB命令 \* @param  array $param      执行的命令 \* @return \\MongoDB\\Driver\\Cursor \*/ functioncommand(array$param) { $cmd=newMongoDB\\Driver\\Command($param); return$this->\_conn->executeCommand($this->\_db,$cmd); } /\*\* \* 获取当前mongoDB Manager \* @return MongoDB\\Driver\\Manager \*/ functiongetMongoManager() { return$this->\_conn; } }