MongoDB 之CURD操作

来源:互联网 发布:小猪cms破解版 编辑:程序博客网 时间:2024/05/02 02:51
class DB{    //public static $db ='runoob';    public static $link = false;    public static $ReadPreference = false;    public static $WriteConcern = false;    public function getMongoDB(){        try {            if(!self::$link){                $Mconfig = \Yaf_Application::app()->getConfig()->mongo;                $link = 'mongodb://'.$Mconfig->user.':'.$Mconfig->passwd.'@'.$Mconfig->ip.':'.$Mconfig->port;                $linkInfo = new MongoDB\Driver\Manager($link);            }            return self::$link = $linkInfo;        } catch (Exception $e) {            print $e->getMessage();exit();        }    }    //CURD 批量操作类    public static function BulkWrite()    {        return new MongoDb\Driver\BulkWrite();    }    //数据库查询    public static function  MQuery($filer,$option)    {         $query = new MongoDb\Driver\Query($filer,$option);       $mongo = self::getMongoDB();       $data = $mongo -> executeQuery('runoob.runoob',$query);       return $data->toArray();    }    //序列化数据    public static function WriteConcern($data)    {        if (!self::$WriteConcern) {              self::$WriteConcern  = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);        }        return self::$WriteConcern;    }    //    public static function ReadPreference()    {        if(!self::$ReadPreference){            self::$readPreference = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY_PREFERRED);        }        return self::$ReadPreference;    }     //操作db 的 集合    public static function MSDBC($db , $collectionName)    {        return $db.'.'.$collectionName;    }    //插入数据    public static function MInsert($data,$db_collection)    {         $mongo = self::getMongoDB();         $BulkWrite = self::BulkWrite();         $BulkWrite->insert($data);         return  $mongo->executeBulkWrite($db_collection,$BulkWrite);      }    //查询数据    public static function MSelect($filter,$options,$db,$collectionName)    {        $this->MQuery($filter,$options,$db,$collectionName);    }    //修改数据    public static function Mupdate($where,$db_collection,$data, $extra = array('multi' => false, 'upsert' => false))    {           $mongo  = self::getMongoDB();        $BulkWrite = self::BulkWrite();        $writeConcern = self::WriteConcern();        //$BulkWrite->update($where,$data,$extra);['$set' =>['key1'=>'key1__value1__test']]        $BulkWrite->update($where,$data,$extra);        return $mongo->executeBulkWrite($db_collection,$BulkWrite,$writeConcern);    }    //删除数据    public static function MDelect($where,$db_collection,$extra=array('limit' => 1))    {        $mongo = self::getMongoDB();        $BulkWrite = self::BulkWrite();        $writeConcern = self::WriteConcern();        $BulkWrite->delete($where,$extra);        return $mongo ->executeBulkWrite($db_collection,$BulkWrite,$writeConcern);    }   }
##test $Mon = new DB();      //$mongodb = $Mon ->getMongoDB();      //$where = ['title'=>'军事新闻'];      //$data = $Mon->MQuery($where);      //$data =  ['key1'=>'test1','key2'=>'value'];      $db_collection =  'runoob.runoob';      //var_dump( $Mon ->MInsert($data,$db_collection));      //seaslog::info();      //$where = ['key1'=>'test1'];      //var_dump($Mon -> MDelect($where,$db_collection));      $where = ['key1' =>'3407aaaaaaaaa'];      $data = ['$set' =>['key1'=>'3434343434'] ];      var_dump($Mon ->Mupdate($where,$db_collection,$data));
0 0
原创粉丝点击