首頁  >  文章  >  後端開發  >  php怎樣使用momgodb事務

php怎樣使用momgodb事務

(*-*)浩
(*-*)浩原創
2019-09-04 16:19:493214瀏覽

MongoDB的操作一直是透過mongo客戶端進程,進行的操作。但現實中,我們對MOngoDB資料的操作,往往是透過對應的程式實現的,如php、java或是Python等。那麼如何在php中操作MongoDB呢?

php怎樣使用momgodb事務

在PHP中設定MongoDB

在php.ini設定MongoDB相當簡單,只需要加入下列程式碼即可

extension=php_mongo.dll

主要注意的是php_mongo.dll版本必須和目前php版本想對應。否則會出現不相容錯誤。 (建議學習:PHP影片教學

關於php_mongo.dll下載可以到http://pecl.php.net/package/mongo 上下載。裡面提供了很多版本可供選擇。

在PHP中連接MongoDB

首先得開啟MongoDB服務

我們都知道,在php中連接Mysql資料庫,我們可以使用Mysqli或者Pdo類,那麼連接MongoDB是否也有對應的類別呢?答案是肯定得。這個類別就是MongoClient。它是PHP 和 MongoDB 的連接管理器,負責於建立和管理連線。類別的結構如下:

MongoClient {
/* 常量 */
const string VERSION ;
const string DEFAULT_HOST = "localhost" ;
const int DEFAULT_PORT = 27017 ;
const string RP_PRIMARY = "primary" ;
const string RP_PRIMARY_PREFERRED = "primaryPreferred" ;
const string RP_SECONDARY = "secondary" ;
const string RP_SECONDARY_PREFERRED = "secondaryPreferred" ;
const string RP_NEAREST = "nearest" ;
/* 属性 */
public boolean $connected = FALSE ;
public string $status = NULL ;
protected string $server = NULL ;
protected boolean $persistent = NULL ;
/* 方法 */
public __construct ([ string $server = "mongodb://localhost:27017" [, array $options = array("connect" => TRUE) ]] )
public bool close ([ boolean|string $connection ] )
public bool connect ( void )
public array dropDB ( mixed $db )
public MongoDB __get ( string $dbname )
public static array getConnections ( void )
public array getHosts ( void )
public array getReadPreference ( void )
public array getWriteConcern ( void )
public bool killCursor ( string $server_hash , int|MongoInt64 $id )
public array listDBs ( void )
public MongoCollection selectCollection ( string $db , string $collection )
public MongoDB selectDB ( string $name )
public bool setReadPreference ( string $read_preference [, array $tags ] )
public bool setWriteConcern ( mixed $w [, int $wtimeout ] )
public string __toString ( void )
}

在PHP中查詢MongoDB資料

在PHP的MongoDB擴充模組中,提供了MongoCollection來進行資料的CURD運算。

以上是php怎樣使用momgodb事務的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn