コアクラス群目次
 警告
    
  この拡張モジュールは非推奨です。 かわりに MongoDB 拡張モジュールを使うべきです。 これらのクラスは、ドライバの最も重要な部分です。 MongoClient クラス警告
    
  このクラスを定義している拡張モジュールは非推奨です。 かわりに MongoDB 拡張モジュールを使うべきです。 このクラスの代替として、以下が使えます。 
 はじめにPHP と MongoDB の接続を管理します。 このクラスを使って、接続を作ったり管理したりします。典型的な使いかたは、このようになります。 例1 MongoClient の基本的な使いかた 
<?php接続の作成に関する詳細な情報は、 MongoClient::__construct および 接続 のセクションを参照ください。 クラス概要MongoClient 
    
    
     
       class MongoClient
     
     { 
    
    /* 定数 */ 
    
     const
     string
     MongoClient::VERSION
    ; 
    
     const
     string
     MongoClient::DEFAULT_HOST
      = "localhost"
    ; 
    
     const
     int
     MongoClient::DEFAULT_PORT
      = 27017
    ; 
    
     const
     string
     MongoClient::RP_PRIMARY
      = "primary"
    ; 
    
     const
     string
     MongoClient::RP_PRIMARY_PREFERRED
      = "primaryPreferred"
    ; 
    
     const
     string
     MongoClient::RP_SECONDARY
      = "secondary"
    ; 
    
     const
     string
     MongoClient::RP_SECONDARY_PREFERRED
      = "secondaryPreferred"
    ; 
    
     const
     string
     MongoClient::RP_NEAREST
      = "nearest"
    ; 
    /* プロパティ */ 
    
     public
     bool
     $connected
      =  
    false
    ;
     public
     string
     $status
      =  
    null
    ;
     protected
     string
     $server
      =  
    null
    ;
     protected
     bool
     $persistent
      =  
    null
    ;/* メソッド */ 
    
   public  __construct
    ([ string  
    $server = "mongodb://localhost:27017"
   [, array $options = array("connect" => true)
   [, array $driver_options
  ]]] )
   public bool Mongo::close
    ([ boolstring  
$connection
  ] )
   public bool Mongo::connect
    ( void
   ) 
   public array dropDB
    ( mixed  
$db
   )
   public MongoDB Mongo::__get
    ( string  
$dbname
   )
   public static array getConnections
    ( void
   ) 
   public array Mongo::getHosts
    ( void
   ) 
   public array getReadPreference
    ( void
   ) 
   public array getWriteConcern
    ( void
   ) 
   public bool killCursor
    ( string  
$server_hash
   , intMongoInt64 $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 Mongo::__toString
    ( void
   ) 
   }定義済み定数MongoClient 定数
 フィールド
 参考
 MongoDB クラスはじめにこのクラスのインスタンスを使用してデータベースとのやりとりを行います。 データベースを取得するには、このようにします。 例1 データベースの選択 
<?phpあまり一般的ではありませんが、 "null"、"[x,y]"、"3"、"\""、"/" などは正しい形式のデータベース名です。 コレクション名とは異なり、データベース名には "$" を含めてもかまいません。 クラス概要MongoDB 
    
     
       class MongoDB
     
     { 
    
    /* 定数 */ 
    
     const
     int
     MongoDB::PROFILING_OFF
      = 0
    ; 
    
     const
     int
     MongoDB::PROFILING_SLOW
      = 1
    ; 
    
     const
     int
     MongoDB::PROFILING_ON
      = 2
    ; 
    /* フィールド */ 
    
     public
     int
     $w
      = 1
    ; 
    
     public
     int
     $wtimeout
      = 10000
    ; 
    /* メソッド */ 
    
   public array authenticate
    ( string  
$username
   , string $password
   )
   public array command
    ( array  
$command
   [, array $options = array()
   [, string &$hash
  ]] )
   public  __construct
    ( MongoClient  
$conn
   , string $name
   )
   public MongoCollection createCollection
    ( string  
$name
   [, array $options
  ] )
   public array createDBRef
    ( string  
$collection
   , mixed $document_or_id
   )
   public array drop
    ( void
   ) 
   public array dropCollection
    ( mixed  
$coll
   )
   public array execute
    ( mixed  
$code
   [, array $args = array()
  ] )
   public bool forceError
    ( void
   ) 
   public MongoCollection __get
    ( string  
$name
   )
   public array getCollectionInfo
    ([ array  
$options = array()
  ] )
   public array getCollectionNames
    ([ array  
$options = array()
  ] )
   public array getDBRef
    ( array  
$ref
   )
   public MongoGridFS getGridFS
    ([ string  
$prefix = "fs"
  ] )
   public int getProfilingLevel
    ( void
   ) 
   public array getReadPreference
    ( void
   ) 
   public bool getSlaveOkay
    ( void
   ) 
   public array getWriteConcern
    ( void
   ) 
   public array lastError
    ( void
   ) 
   public array listCollections
    ([ array  
$options = array()
  ] )
   public array prevError
    ( void
   ) 
   public array repair
    ([ bool  
$preserve_cloned_files = false
   [, bool $backup_original_files = false
  ]] )
   public array resetError
    ( void
   ) 
   public MongoCollection selectCollection
    ( string  
$name
   )
   public int setProfilingLevel
    ( int  
$level
   )
   public bool setReadPreference
    ( string  
$read_preference
   [, array $tags
  ] )
   public bool setSlaveOkay
    ([ bool  
$ok = true
  ] )
   public bool setWriteConcern
    ( mixed  
$w
   [, int $wtimeout
  ] )
   public string __toString
    ( void
   ) 
   }定義済み定数MongoDB ログレベル
 フィールド
 参考MongoDB コアドキュメントの » データベース を参照ください。 MongoCollection クラスはじめにMongoDB のコレクションを表します。 コレクション名は ASCII セット内の任意の文字を使用できます。たとえば ""、"..."、"my collection" そして "*&#@" といった名前をつけることができます。 ユーザー定義のコレクション名には $ 記号を含めることができません。システムコレクションの中には $ を名前に使うものがあります (local.oplog.$main など) が、 これは予約文字です。名前に $ を含むコレクションを作ったり使ったりしようとすると、 MongoDB が assert を出します。 クラス概要MongoCollection 
    
     
       class MongoCollection
     
     { 
    /* 定数 */ 
    
     const
     int
     MongoCollection::ASCENDING
      = 1
    ; 
    
     const
     int
     MongoCollection::DESCENDING
      = -1
    ; 
    
    /* フィールド */ 
    
     public
     MongoDB
     $db
      =  
    null
    ;
     public
     int
     $w
    ; 
    
     public
     int
     $wtimeout
    ; 
    /* メソッド */ 
    
   public array aggregate
    ( array  
$pipeline
   [, array $options
  ] )
   public MongoCommandCursor aggregateCursor
    ( array  
$command
   [, array $options
  ] )
   public mixed batchInsert
    ( array  
$a
   [, array $options = array()
  ] )
   public  __construct
    ( MongoDB  
$db
   , string $name
   )
   public int count
    ([ array  
$query = array()
   [, array $options = array()
  ]] )
   public array createDBRef
    ( mixed  
$document_or_id
   )
   public bool createIndex
    ( array  
$keys
   [, array $options = array()
  ] )
   public array deleteIndex
    ( stringarray  
$keys
   )
   public array deleteIndexes
    ( void
   ) 
   public arrayfalse distinct
    ( string  
$key
   [, array $query
  ] )
   public array drop
    ( void
   ) 
   public bool ensureIndex
    ( stringarray  
$key|keys
   [, array $options = array()
  ] )
   public MongoCursor find
    ([ array  
$query = array()
   [, array $fields = array()
  ]] )
   public array findAndModify
    ( array  
$query
   [, array $update
   [, array $fields
   [, array $options
  ]]] )
   public array findOne
    ([ array  
$query = array()
   [, array $fields = array()
   [, array $options = array()
  ]]] )
   public MongoCollection __get
    ( string  
$name
   )
   public array getDBRef
    ( array  
$ref
   )
   public array getIndexInfo
    ( void
   ) 
   public string getName
    ( void
   ) 
   public array getReadPreference
    ( void
   ) 
   public bool getSlaveOkay
    ( void
   ) 
   public array getWriteConcern
    ( void
   ) 
   public array group
    ( mixed  
$keys
   , array $initial
   , MongoCode $reduce
   [, array $options = array()
  ] )
   public boolarray insert
    ( arrayobject  
$document
   [, array $options = array()
  ] )
   public array[MongoCommandCursor] parallelCollectionScan
    ( int  
$num_cursors
   )
   public boolarray remove
    ([ array  
$criteria = array()
   [, array $options = array()
  ]] )
   public mixed save
    ( arrayobject  
$document
   [, array $options = array()
  ] )
   public bool setReadPreference
    ( string  
$read_preference
   [, array $tags
  ] )
   public bool setSlaveOkay
    ([ bool  
$ok = true
  ] )
   public bool setWriteConcern
    ( mixed  
$w
   [, int $wtimeout
  ] )
   static protected string toIndexString
    ( mixed  
$keys
   )
   public string __toString
    ( void
   ) 
   public boolarray update
    ( array  
$criteria
   , array $new_object
   [, array $options = array()
  ] )
   public array validate
    ([ bool  
   }$scan_data = false
  ] )定義済み定数
 フィールド
 参考MongoDB コアドキュメントの » コレクション を参照ください。 MongoCursor クラスはじめにカーソルは、データベースクエリの結果を反復処理するときに使います。 たとえば、データベースに問い合わせたすべての結果を見るには次のようにします。 例1 MongoCursor の基本的な使いかた 
<?php一般に、カーソルを作るときに MongoCursor のコンストラクタを使うことはありません。 新しいカーソルを作るには、(上の例のように) MongoCollection::find をコールします。 上の例で、仮に $collection が 50GB のコレクションだったとしましょう。そんなときに、 結果をすべて一度にメモリに読み込もうとは思わないでしょう。 そんなときのためにカーソルが存在します。 カーソルを使えば、クライアント側から少しずつコレクションにアクセスできるのです。 結果セットが大規模になっても、それを反復処理して 一度にメモリに取り込むデータ量を数メガバイト程度にすることができます。 たとえばこのようにします。 例2 MongoCursor の反復処理 
<?phpカーソルにデータベースの結果が "含まれている" わけではないことに注意しましょう。 カーソルは単に結果を管理するだけのものです。つまり、 (var_dump や print_r で) カーソルの内容を出力してもドキュメントは見えません。 単にカーソルオブジェクトが表示されるだけです。 ドキュメント自身を取得するには、上のメソッドのいずれかを使います。 カーソルのステージMongoCursor には二段階の「ライフステージ」があります。 クエリ前とクエリ後です。 カーソルを作った時点ではまだデータベースに接続していません。 つまり、クエリ前の段階です。この段階では、クエリに何をしてほしいのかをさらに指定することができます。 取得数の制限や読み飛ばし、ソートなどのオプションを指定できるのです。 クライアントが結果を取得しようとする (直接的あるいは間接的に MongoCursor::next をコールする) と、 そのカーソルはクエリ後の段階に進みます。 
 例3 MongoCursor へのオプションの追加 
<?phpクラス概要MongoCursor 
    
    
     
       class MongoCursor
     
     implements 
      MongoCursorInterface
     
     , 
      Iterator
     
     { 
    
    
    /* 静的フィールド */ 
    
     static
     bool
     $slaveOkay
      =  
    false
    ;
     static
     int
     $timeout
      = 30000
    ; 
    /* メソッド */ 
    
   public MongoCursor addOption
    ( string  
$key
   , mixed $value
   )
   public MongoCursor awaitData
    ([ bool  
$wait = true
  ] )
   public MongoCursor batchSize
    ( int  
$batchSize
   )
   public  __construct
    ( MongoClient  
$connection
   , string $ns
   [, array $query = array()
   [, array $fields = array()
  ]] )
   public int count
    ([ bool  
$foundOnly = false
  ] )
   public array current
    ( void
   ) 
   public bool dead
    ( void
   ) 
   protected void doQuery
    ( void
   ) 
   public array explain
    ( void
   ) 
   public MongoCursor fields
    ( array  
$f
   )
   public array getNext
    ( void
   ) 
   public array getReadPreference
    ( void
   ) 
   public bool hasNext
    ( void
   ) 
   public MongoCursor hint
    ( mixed  
$index
   )
   public MongoCursor immortal
    ([ bool  
$liveForever = true
  ] )
   public array info
    ( void
   ) 
   public stringint key
    ( void
   ) 
   public MongoCursor limit
    ( int  
$num
   )
   public MongoCursor maxTimeMS
    ( int  
$ms
   )
   public array next
    ( void
   ) 
   public MongoCursor partial
    ([ bool  
$okay = true
  ] )
   public void reset
    ( void
   ) 
   public void rewind
    ( void
   ) 
   public MongoCursor setFlag
    ( int  
$flag
   [, bool $set = true
  ] )
   public MongoCursor setReadPreference
    ( string  
$read_preference
   [, array $tags
  ] )
   public MongoCursor skip
    ( int  
$num
   )
   public MongoCursor slaveOkay
    ([ bool  
$okay = true
  ] )
   public MongoCursor snapshot
    ( void
   ) 
   public MongoCursor sort
    ( array  
$fields
   )
   public MongoCursor tailable
    ([ bool  
$tail = true
  ] )
   public MongoCursor timeout
    ( int  
$ms
   )
   public bool valid
    ( void
   ) 
   }静的変数
 参考MongoDB コアドキュメントの » カーソル を参照ください。 The MongoCursorInterface interfaceはじめにInterface for cursors, which can be used to iterate through results of a database query or command. This interface is implemented by the MongoCursor and MongoCommandCursor classes. 
 クラス概要MongoCursorInterface 
    
    
     
       class MongoCursorInterface
     
     
      extends
       Iterator
     
     { 
    
    /* メソッド */ 
    
   abstract public MongoCursorInterface batchSize
    ( int  
$batchSize
   )
   abstract public bool dead
    ( void
   ) 
   abstract public array getReadPreference
    ( void
   ) 
   abstract public array info
    ( void
   ) 
   abstract public MongoCursorInterface setReadPreference
    ( string  
$read_preference
   [, array $tags
  ] )
   abstract public MongoCursorInterface timeout
    ( int  
    $ms
   )/* 継承したメソッド */ 
    
   abstract public mixed Iterator::current
    ( void
   ) 
   abstract public scalar Iterator::key
    ( void
   ) 
   abstract public void Iterator::next
    ( void
   ) 
   abstract public void Iterator::rewind
    ( void
   ) 
   abstract public bool Iterator::valid
    ( void
   ) 
   }The MongoCommandCursor classはじめにA command cursor is similar to a MongoCursor except that you use it for iterating through the results of a database command instead of a normal query. Command cursors are useful for iterating over large result sets that might exceed the document size limit (currently 16MB) of a single MongoDB::command response. While you can create command cursors using MongoCommandCursor::__construct or the MongoCommandCursor::createFromDocument factory method, you will generally want to use command-specific helpers such as MongoCollection::aggregateCursor. Note that the cursor does not "contain" the database command's results; it just manages iteration through them. Thus, if you print a cursor (f.e. with var_dump or print_r), you will see the cursor object but not the result documents. Cursor StagesA MongoCommandCursor has two "life stages": pre- and post- command. When a cursor is created, it has not yet contacted the database, so it is in its pre-command state. When the client first attempts to get a result (by calling MongoCommandCursor::rewind, directly or indirectly), the cursor moves into the post-command state. The command cursor's batch size and socket timeout may be configured in both the pre- and post- command states. 
 例1 Adding options to MongoCommandCursor 
<?phpクラス概要MongoCommandCursor 
    
    
     
       class MongoCommandCursor
     
     implements 
      MongoCursorInterface
     
     , 
      Iterator
     
     { 
    
    
    /* メソッド */ 
    
   public MongoCommandCursor batchSize
    ( int  
$batchSize
   )
   public  __construct
    ( MongoClient  
$connection
   , string $ns
   , array $command = array()
   )
   public static MongoCommandCursor createFromDocument
    ( MongoClient  
$connection
   , string $hash
   , array $document
   )
   public array current
    ( void
   ) 
   public bool dead
    ( void
   ) 
   public array getReadPreference
    ( void
   ) 
   public array info
    ( void
   ) 
   public int key
    ( void
   ) 
   public void next
    ( void
   ) 
   public array rewind
    ( void
   ) 
   public MongoCommandCursor setReadPreference
    ( string  
$read_preference
   [, array $tags
  ] )
   public MongoCommandCursor timeout
    ( int  
$ms
   )
   public bool valid
    ( void
   ) 
   }参考
  |