class DB
{
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 ();
}
}
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 ;
}
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 );
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 );
}
}
$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 ));