Mongo_Db クラス・メソッド
Mongo_Db クラスは、MongoDBデータベースとの対話を可能にします。
Mongo_Db クラスは、MySQLのようなデータベースの扱いに似ているものの、いくつかの点で、はっきりとした違いがあります。
すべてのソートオプション(order by や indexes) は、DESCの意味として、-1、 false、 'desc' または 'DESC' を使用します。 それら以外の値はすべて ASC として扱われます。
instance($name = 'default')
instance メソッドは、 新しい Mongo_Db インスタンスを返却します。
使用したい設定、データベース用の $name パラメータを指定して使用してください。
デフォルトでは、 $name は 'default' が使用されます。そのため、db.php の設定として、'default' が設定されている必要があります。
Mongo_Db 設定の詳細はこちらを参照ください。
静的 |
はい |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$collection |
string |
'default'
|
使用したい config group の名前を指定します。 |
|
返り値 |
新しい Mongo_Db インスタンス |
例外 |
Mongo_DbException 指定されたインスタンス名に対応する設定が見つからなかった場合。 |
例 |
// Get the default group
$mongodb = \Mongo_Db::instance();
// Get a custom group
$mongodb = \Mongo_Db::instance('my_group');
|
get($collection = "")
get メソッドは、 コレクションの検索結果を返却します。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$collection |
string |
''
|
コレクション名 |
|
返り値 |
結果の配列を含む stdClass オブジェクト |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$result = $mongodb->get('users');
foreach($result as $user)
{
// Do something with $user
}
|
get_cursor($collection = "")
get_cursor メソッドは、指定したコレクションから MongoDB カーソル を返却します。通常の get() メソッドに対するアドバンテージは、巨大なデータセットを処理する際、一度にすべてロードするのではなく、アクセスごとに都度メモリ上にロードされることです。これは、長い時間かかる処理の際にとても有用です。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
Description |
$collection |
string |
''
|
コレクション名 |
|
返り値 |
A MongoCursor object. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$result = $mongodb->get_cursor('documents');
foreach($result as $document)
{
// Do something with $document
}
|
get_one($collection = "")
get_one メソッドは、コレクションから1つの結果を返します。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$collection |
string |
''
|
コレクション名 |
|
返り値 |
A stdClass object containing the single result array. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$user = $mongodb->get_one('users');
|
get_where($collection = "", $where = array(), $limit = 99999)
get_where メソッドは、指定した検索条件、取得数上限のコレクションの結果を返却します。
検索条件は、配列で指定することができます。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$collection |
string |
''
|
コレクション名 |
$where |
array |
array()
|
An array of where statements. |
|
返り値 |
A stdClass object containing the result arrays. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
// 'active' が "yes" の条件に合致する、最初の 10 件を取得します。
$result = $mongodb->get_where('users', array(
'active' => 'yes',
), 10);
foreach($result as $user)
{
// Do something with $user
}
|
select($includes = array(), $excludes = array())
select メソッドは、クエリにより、どのフィールドの値を取得するか、もしくは除外するか を指定します。
除外リストを使用する場合には、取得リストは空である必要があります。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$includes |
array |
array()
|
取得したいフィールドの配列 |
$where |
array |
array()
|
除外したいフィールドの配列 |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
// Just select these fields
$mongodb->select(array(
'name',
'surname',
'email',
));
$users = $mongodb->get('users');
// Or use the exclude array
$mongodb = \Mongo_Db::instance();
// Just select these fields
$mongodb->select(array(), array(
'not_this',
'or_this',
));
$users = $mongodb->get('users');
|
where($wheres = array())
where メソッドは、'where is equal to' ステートメントをセットします。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$wheres |
array |
array()
|
キーをフィールド名、値を検索文字列とした、連想配列 |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance.
$mongodb = \Mongo_Db::instance();
$mongodb->where(array(
'name' => 'John',
'surname' => 'Doe',
));
$users = $mongodb->get('users');
|
or_where($wheres = array())
or_where メソッドは、'or where is equal to' ステートメントをセットします。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$wheres |
array |
array()
|
キーをフィールド名、値を検索文字列とした、連想配列 |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where(array(
'name' => 'John',
'surname' => 'Doe',
))->or_where(array(
'name' => 'Jim',
));
$users = $mongodb->get('users');
|
where_in($field = "", $in = array())
where_in メソッドは、'where in' ステートメントをセットします。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$field |
string |
''
|
The field to query. |
$in |
array |
array()
|
An array of possible values. |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_in('name', array(
'John', 'James', 'Frank', 'Dave',
));
$users = $mongodb->get('users');
|
where_in_all($field = "", $in = array())
where_in_all メソッドは、'all where in' ステートメントをセットします。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$field |
string |
''
|
The field to query. |
$in |
array |
array()
|
An array of possible values. |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_in_all('name', array(
'John', 'James', 'Frank', 'Dave',
));
$users = $mongodb->get('users');
|
where_not_in($field = "", $in = array())
where_not_in メソッドは、'where not in' ステートメントをセットします。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$field |
string |
''
|
The field to query. |
$in |
array |
array()
|
An array of possible values. |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_not_in('name', array(
'John', 'James', 'Frank', 'Dave',
));
$users = $mongodb->get('users');
|
where_gt($field = "", $x)
where_gt メソッドは、 'where greater than' ステートメントをセットします。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$field |
string |
''
|
The field to query. |
$x |
mixed |
required |
The value to compare it to. |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_gt('age', 12);
$users = $mongodb->get('users');
|
where_gte($field = "", $x)
where_gte メソッドは、 'where greater than or equal to' ステートメントをセットします。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$field |
string |
''
|
The field to query. |
$x |
mixed |
required |
The value to compare it to. |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_gte('age', 18);
$users = $mongodb->get('users');
|
where_lt($field = "", $x)
where_lt メソッドは、 'where lower than' ステートメントをセットします。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$field |
string |
''
|
The field to query. |
$x |
mixed |
required |
The value to compare it to. |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_lt('age', 120);
$users = $mongodb->get('users');
|
where_lte($field = "", $x)
where_lte メソッドは、 'where lower than or equal to' ステートメントをセットします。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$field |
string |
''
|
The field to query. |
$x |
mixed |
required |
The value to compare it to. |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_lt('age', 120);
$users = $mongodb->get('users');
|
where_between($field = "", $x)
where_between メソッドは、 'where between' ステートメントをセットします。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$field |
string |
''
|
The field to query. |
$x |
mixed |
required |
The low value to compare it to. |
$y |
mixed |
required |
The high value to compare it to. |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_between('age', 10, 20);
$users = $mongodb->get('users');
|
where_between_ne($field = "", $x)
where_between_ne メソッドは、 'where between and not equal to' ステートメントをセットします。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$field |
string |
''
|
The field to query. |
$x |
mixed |
required |
The low value to compare it to. |
$y |
mixed |
required |
The high value to compare it to. |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_between_ne('age', 10, 20);
$users = $mongodb->get('users');
|
where_ne($field = "", $x)
where_ne メソッドは、'where not equal to' ステートメントをセットします。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$field |
string |
''
|
The field to query. |
$x |
mixed |
required |
The low value to compare it to. |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->where_ne('name', 'Allerd');
$users = $mongodb->get('users');
|
like($field = '', $value = '', $flags = 'i', $enable_start_wildcard = true, $enable_end_wildcard = true)
like メソッドは、'like' ステートメントをセットします。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$field |
string |
''
|
The field to query. |
$value |
mixed |
''
|
The value to compare it to. |
$flags |
string |
'i'
|
Allows you to set typical regular expression flags:
- i = case insensitive
- m = multiline
- x = can contain comments
- l = locale
- s = dotall, "." matches everything, including newlines
- u = match unicode
|
$enable_start_wildcard |
bool |
false
|
true がセットされた場合、the beginning of the string must match. |
$enable_end_wildcard |
bool |
false
|
When set to true the ending of the string must match. |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->like('name', 'fran', 'im', true);
$users = $mongodb->get('users');
|
order_by($fields = array())
order_by メソッドは、結果の並び順を指定します。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$fields |
array |
array()
|
ソート順を降順にセットしたい場合、 -1、 FALSE、 'desc' もしくは、 'DESC' を指定します。それ以外は1 (昇順)となります。 |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->order_by(array(
'age' => 'asc',
'name' => 'desc'
));
$users = $mongodb->get('users');
|
limit($x = 99999)
limit メソッドは、取得したいアイテム数を指定します。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$x |
number |
99999
|
The number of items to fetch. |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
// Get the 10 oldest
$mongodb->order_by(array(
'age' => 'desc',
))->limit(10);
$users = $mongodb->get('users');
|
offset($x = 0)
The offset method sets the number of items skip.
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$x |
number |
0
|
The number of items to skip. |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
// Get row 10 to 15
$mongodb->order_by(array(
'age' => 'desc',
))->limit(5)->offset(10);
$users = $mongodb->get('users');
|
count($collection = '', $foundonly = false)
count メソッドは、結果数を返却します。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$collection |
string |
''
|
The collection (table) to use. |
$foundonly |
boolean |
false
|
if true, it will take the current limit and skip selections into account. |
|
返り値 |
The number of rows. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
// Get the amount of users.
$num_users = $mongodb->count('users');
|
insert($collection = '', $insert = array())
insert メソッドは、指定されたコレクションにレコードを追加します。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$collection |
string |
''
|
The collection (table) to use. |
$insert |
array |
array()
|
The values to insert |
|
返り値 |
The insert id, or false on failure. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
// Insert a new user
$insert_id = $mongodb->insert('users', array(
'name' => 'John',
'surname' => 'Doe',
'email' => 'dont.em@ilme.com',
));
|
update($collection = "", $data = array(), $options = array())
update メソッドは、指定されたコレクションをアップデートします。
If you do not set the "multiple" option to true only the first encountered record will be updated.
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$collection |
string |
''
|
The collection (table) to use. |
$data |
array |
array()
|
The new values. |
$options |
array |
array()
|
Supply optional extra options
(?)
to the update command.
|
|
返り値 |
true on success |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
// Update a user
$bool = $mongodb->where(array('id' => $an_id))->update('users', array(
'name' => 'John',
'surname' => 'Doe',
'email' => 'john@doe.com',
));
|
update_all($collection = "", $data = array())
update_all メソッドは、指定されたコレクションのすべてのレコードをアップデートします。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$collection |
string |
''
|
The collection (table) to use. |
$data |
array |
array()
|
The new values. |
|
返り値 |
true on success |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
// Update Spammer's surname.
$bool = $mongodb->where('name', 'Spammer')->update_all('users', array(
'surname' => 'Don\'t believe us.',
));
|
delete($collection = "")
delete メソッドは、与えられたコレクションの最初のレコードを削除します。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$collection |
string |
''
|
The collection (table) to use. |
|
返り値 |
true on success |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
// Delete a user named Spammer
$bool = $mongodb->where(array('name' => 'Spammer'))->delete('users');
|
delete_all($collection = "")
delete_all メソッドは、与えられたコレクションのすべてのレコードを削除します。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$collection |
string |
''
|
The collection (table) to use. |
|
返り値 |
true on success |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
// Delete all users named Spammer
$bool = $mongodb->where(array('name' => 'Spammer'))->delete_all('users');
|
command($query = array())
command メソッドは、MongoDB コマンドを実行します。コマンドについての詳細については、こちらをご参照ください。
the MongoDB docs.
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$query |
array |
array()
|
An array of command parameters. |
|
返り値 |
The command result |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->command(array(
'geoNear' => 'buildings',
'near' => array(53.228482, -0.547847),
'num' => 10,
'nearSphere' => true
));
|
add_index($collection = "", $keys = array(), $options = array())
add_index メソッドはコレクションのキーにインデックスを追加します。 指定可能なオプションについては、こちらをご覧ください。
php.net.
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$collection |
string |
''
|
The collection (table) to use. |
$keys |
array |
array()
|
An associative array with fields and sorting directions. |
$options |
array |
array()
|
An array with optional settings. |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->add_index('my_collection', array(
'first_name' => 'ASC',
'last_name' => -1)
, array(
'unique' => true
));
|
remove_index($collection = "", $keys = array())
remove_index メソッドは、コレクションから一つ以上のインデックスを削除します。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$collection |
string |
''
|
The collection (table) to use. |
$keys |
array |
array()
|
An associative array with fields (and sorting directions). |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->remove_index('my_collection', array(
'first_name' => 'ASC',
'last_name' => -1)
));
// or
$mongodb->remove_index('first_name');
|
remove_all_indexes($collection = "")
remove_all_indexes メソッドは、コレクションのすべてのインデックスを削除します。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$collection |
string |
''
|
The collection (table) to use. |
|
返り値 |
The current Mongo_Db instance. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$mongodb->remove_all_indexes('my_collection');
|
list_indexes($collection = "")
list_indexes メソッドは、インデックス名を配列で返却します。
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$collection |
string |
''
|
The collection (table) to use. |
|
返り値 |
An array of index names for this collection. |
例 |
// Get an instance
$mongodb = \Mongo_Db::instance();
$indexes = $mongodb->list_indexes('my_collection');
|
get_collection($collection = "")
get_collection メソッドは、MongoDB PHP Driver から、MongoCollection オブジェクトを返却します。(?)
静的 |
No |
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$collection |
string |
''
|
The collection (table) to use. |
|
返り値 |
A MongoCollection object from the core MongoDB PHP Driver. |
例 |
// Get a collection
$mongodb = \Mongo_Db::instance();
$my_collection = $mongodb->get_collection('my_collection');
$my_collection->update(array('_id' => $id), array('$push' => array('array.to.add.to' => $data)));
This was put in place due to the inherent lack of support for update modifier operation.
|