php Mongo
来源:互联网 发布:剑网三插件数据 编辑:程序博客网 时间:2024/05/18 14:25
<?php /** * Created by PhpStorm. * User: yangyafei * Date: 14-8-26 * Time: 下午3:54 */ class Mongoo extends MongoDB{ private $mongoClient = null; private $db = null; private $collectionList = array(); public function __construct() { } public function connect() { if ($this->mongoClient) { return; } $config = array( 'DB' => 'mytest', 'Host' => '127.0.0.1', ); $option = array( "db"=>$config["DB"], // "connec"=>true, // "connectionTimeoutMS"=>500 ); if (!empty($config["Username"])) { $options["username"] = $config["Username"]; } if (!empty($config["Password"])) { $options["password"] = $config["Password"]; } $this->mongoClient = new MongoClient("mongodb://{$config["Host"]}", $option); $this->db = $this->mongoClient->selectDB($config["DB"]); parent::__construct($this->mongoClient, $config["DB"]); } //获取连接的服务器 public function getMonClient(){ $this->connect(); return $this->mongoClient; } //获取对象 public function getMGCollection($collectionName){ $this->connect(); if(!isset($this->collectionList[$collectionName])){ $collectionObj = new MongoCollection($this, $collectionName); $this->collectionList[$collectionName] = $collectionObj; } return $this->collectionList[$collectionName]; } /* findOne查找一个满足条件的信息 collection->findOne($collection,array $query(), array $filed() ) $collection查找的对象 array $query条件 array $filed需要的字段 */ public function findOne($collection,array $query = array(),array $fields = array()){ $this->connect(); $mgCollection = $this->getMGCollection($collection); return $mgCollection->findOne($query,$fields); } /* find($collection $query, $sort,$limit,fields) $collection查找的对象 array $query条件 array $filed需要的字段 sort排序 limit用来限制找到的数目 */ public function find($collection,array $query = array(),array $sort = array(),$limit = NULL,array $fields = array()){ $this->connect(); $mgCollection = $this->getMGCollection($collection); $resultObj = $mgCollection->find($query, $fields); //判断整数,返回这个整数 if (is_int($limit)) { $resultObj = $resultObj->limit($limit); //如果不是整数,则去掉一部分 //is_array检查是否是数组,返回bool } else if (is_array($limit) && count($limit) == 2) { $resultObj = $resultObj->limit((int) $limit[1])->skip((int) $limit[0]); } // 排序 if (!empty($sort)) { $resultObj = $resultObj->sort($sort); } return $resultObj; } /* save向对象collection中新的内容 save($collection,$arr) 向对象collection添加arr */ public function save($collection, $new_object) { $this->connect(); $mgCollection = $this->getMGCollection($collection); return $mgCollection->save($new_object); } /* 删除 remove($collection,array contion,array option) 向collection中查找满足添加¥contion的内容进行删除,结果的限制由option来完成 MongoCollection::remove($criteria, $options) $criteria条件,$option输出的限制 */ public function remove($collection, array $criteria = array(), array $options = array()) { $this->connect(); $mgCollection = $this->getMGCollection($collection); return $mgCollection->remove($criteria, $options); } //更新 public function update($collection, array $criteria, $new_object, array $options = array()) { $this->connect(); $mgCollection = $this->getMGCollection($collection); return $mgCollection->update($criteria, $new_object, $options); } /* 计算数目 count($collection, array $query = array(), $limit = 0, $skip = 0) $colection要执行的对象 $query要查询的字段 limit限制的数目 skip开始计数前需要跳过的数目 MongoCollection::count($query,$limit,$skip) 返回文档的个数,跳过$skip个文档,最大限制为limit */ public function count($collection, array $query = array(), $limit = 0, $skip = 0) { $this->connect(); $mgCollection = $this->getMGCollection($collection); return $mgCollection->count($query, $limit, $skip); } public function aggregate($collection, array $pipeline) { $this->connect(); $mgCollection = $this->getMGCollection($collection); return $mgCollection->aggregate($pipeline); } public function gridFS() { $this->connect(); return $this->getGridFS(); } /* 执行指令 MongoDb::command($commands)是来执行一条语句commands */ public function runCommand(array $commands) { $this->connect(); return $this->db->command($commands); } } ?> <?php $a = new Mongoo(); $ans = $a->save("yangyafei",array ("name" =>"A","password"=>"A1")); print_r($ans); echo " save ok"."<br>"; $ans = $a->findOne("yangyafei",array("name"=>"A","password"=>"A1"),array("name"=>true,"password"=>true)); print_r($ans); echo " findOne ok"."<br>"; $sort = array(); $ans = $a->find("yangyafei",array ("name"=>"A","password"=>"A1"),$sort,4,array("name"=>true,"password"=>true)); $ans->snapshot(); foreach($ans as $k=>$v){ print_r(var_dump($v)); } echo " find ok"."<br>"; $ans =$a->remove("yangyafei",array("name"=>"yangyafei","password"=>"yy123"),array("name"=>true,"password"=>true)); print_r($ans); echo "remove ok"."<br>"; $ans =$a->update("yangyafei",array("name"=>"A","password"=>"A1"),array("name"=>"B","password"=>"BB"),array("name"=>true,"password"=>true)); print_r($ans); echo " update ok"."<br>"; $ans =$a->count("yangyafei",array("name"=>"A","password"=>"A1"),5,1); print_r($ans); echo " count ok"."<br>"; ?>
<?php/** * Created by PhpStorm. * User: yangyafei * Date: 14-8-26 * Time: 下午12:15 *//* $conn = new MongoClient(); //创建一个连接 $dbs = $conn->listDBs(); //获得一个包含db信息的数组 eg:$name=MongoClient::listDBs(); $db = $conn->selectCollection("local","yangyafei"); //获取当前连接下,local数据库中的对象yangyafei $db = new MongoDB($conn,"test"); //创建一个当前conn连接下的一个数据库test; $db->drop(); //删除数据库db $db = $conn->local; $string = $db->_tostring(); //获取当前连接,数据库local下的数据库名*//* 获取想要的对象collection a: $conn = new MongoClient(); $coll = $conn->dbName->collName; b; $conn = new MongoClient(); $db = $conn->dbname; $coll = $db->collName; c: $conn = new MongoClient(); $db = $conn->dbname; $coll = $db->selectCollection("collname");*//* 插入数据insert mongoCollection->(array $a,array $option) $a要插入的数组 $option 选项(safe 是否返回操作信息结果,bool类型不用加变量; fsync 是否直接插入到物理硬盘;)*//* $conn = new MongoClient(); $db = $conn->selectDB("local"); $coll = $db->selectCollection("yangyafei"); $collection = array("name"=>"yangyafei","password"=>"yyf123"); $option = array("safe"=>true); $result = $coll->insert($collection,$option); print_r($result);*//* 删除数据remove mongoCollection->remove(array $contion,array $option,) $contion条件 $option (safe 是否返回操作结果 fsync 是否是直接影响到物理硬盘 justOne 是否只影响一条记录)*//* $conn = new MongoClient(); $db = $conn->selectDB("local"); $coll = $db->selectCollection("yangyafei"); $contion = array("name"=>"yangyafei","password"=>"yyf123"); $option = array("safe"=>true,"justOne"=>false); $ans = $coll->remove($contion,$option); print_r($ans); echo "ok\n";*//* 更新数据update mongoCollection->update($contion,$obj,$option) $contion条件 $obj要更新的内容 $option选项(safe 是否返回操作结果;fsync 是否是直接影响到物理硬盘;upsert 是否没有匹配数据就添加一条新的 multiple 是否影响所有符合条件的记录,默认只影响一条)*//* $conn = new MongoClient(); $db = $conn->selectDB("local"); $coll = $db->selectCollection("yangyafei"); $contion = array ("name"=>"yangyafei","password"=>"yyf123",); $obj = array("name"=>"luyanfeng","password"=>"lyf123"); $option = array("safe"=>true,"multiple"=>true); $ans = $coll->update($contion,$obj,$option); print_r($ans); echo "update ok\n";*//* 查询数据find mongoCollection->find($contion,$option); $contion 条件 $option 要获得的属性 返回一个游标记录对象MongoCursor*//* $conn = new MongoClient(); $db = $conn->selectDB("local"); $coll = $db->selectCollection("yangyafei"); $contion = array("name"=>"luyanfeng"); $option = array("name"=>true,"password"=>true); $cursor = $coll->find($contion,$option);*/ //游标对象MongoCursor操作 /*while($cursor->hasNext()){ $ans = $cursor->getNext(); print_r($ans);}*/ //也可以用foreach/* $cursor->snapshot(); foreach($cursor as $k=>$v){ print_r(var_dump($v)); } echo "find ok \n";*/ /* * findone */ $conn = new MongoClient(); $db = $conn->selectDB("local"); $coll = $db->selectCollection("yangyafei"); $contion = array("name"=>"luyanfeng"); $option = array("name"=>true,"password"=>true); $ans = $coll->findOne($contion,$option); print_r($ans); echo "findOne ok\n";?>
0 0
- php Mongo
- php 安装 mongo 扩张
- php配置mongo扩展
- PHP连接mongo
- php 装配mongo扩展
- php mongo 扩展
- PHP mongo扩展
- php扩展mongo数据库
- docker php mongo
- PHP操作mongo数据库
- Mongo PHP Api 之数据库连接
- PHP扩展Mongo简单使用
- Mongo:笔记六(php实例)
- Ubuntu安装php--mongo扩展
- PHP 连接Mongo操作实例
- php安装mongo扩展问题解决
- MONGO
- mongo
- a quick tour of many tools
- cocos2dx-3.0中的物理引擎Box2D
- MySQL学习笔记(十四)自定义函数
- Linux命名管道
- poj1458
- php Mongo
- Android APK签名(针对jdk1.7)
- ActiveMQ In Action 第一章 消息传递和ActiveMQ简介 前言
- LightOj 1123-Trail Maintenance(最小生成树:神级删边)
- 如何设置 C# 程序配置
- 最短路(SPFA算法)(队列思想)B - 畅通工程续
- test
- C++实现简单的排序
- [u-boot]关于star.s中“异常向量”的一个疑问