串連mongo
$connection = new MongoClient (); // 串連到 localhost:27017$connection = new MongoClient ( "mongodb://example.com" ); // 串連到遠程伺服器 (使用預設連接埠: 27017)$connection = new MongoClient ( "mongodb://example.com:65432" ); // 連結到遠程伺服器,使用自訂的連接埠
這個驅動使用了持久串連,並會在下次試圖連結到同一伺服器時重用它。
驗證
// Specifying the username and password in the connection URI (preferred)$m = new MongoClient ( "mongodb:// ${ username } : ${ password } @localhost" );// Specifying the username and password via the options array (alternative)$m = new MongoClient ( "mongodb://localhost" , array( "username" => $username , "password" => $password ));// Specifying the authentication database in the connection URI (preferred)$m = new MongoClient ( "mongodb:// ${ username } : ${ password } @localhost/myDatabase" );// Specifying the authentication database via the options array (alternative)$m = new MongoClient ( "mongodb:// ${ username } : ${ password } @localhost" , array( "db" => "myDatabase" ));
分區(叢集)
$m = new MongoClient ( "mongodb://mongos1.example.com:27017,mongos2.example.com:27017" ));
複製
使用 "replicaSet" 選項指定複製的名字。相同的名字代表在一個叢集裡. 多個伺服器用逗號分割。
// Using multiple servers as the seed list (prefered)$m = new MongoClient ( "mongodb://rs1.example.com:27017,rs2.example.com:27017/?replicaSet=myReplSetName" ));// Using one server as the seed list $m = new MongoClient ( "mongodb://rs1.example.com:27017" , array( "replicaSet" => "myReplSetName" ));// Using multiple servers as the seed list$m = new MongoClient ( "mongodb://rs1.example.com:27017,rs2.example.com:27017" , array( "replicaSet" => "myReplSetName" ));
擷取資料庫執行個體
$connection = new MongoClient ();$db = $connection -> dbname ;
擷取集合執行個體
$connection = new MongoClient ();$db = $connection -> baz ;// select a collection:$collection = $db -> foobar ;// or, directly selecting a database and collection:$collection = $connection -> baz -> foobar ;
插入一個文檔
使用 MongoCollection::insert() 方法:
$doc = array( "name" => "MongoDB" , "type" => "database" , "count" => 1 , "info" => (object)array( "x" => 203 , "y" => 102 ), "versions" => array( "0.9.7" , "0.9.8" , "0.9.9" ));$collection -> insert ( $doc );
使用 MongoCollection::findOne() 方法
$document = $collection -> findOne ();
添加多個文檔:
for ( $i = 0 ; $i < 100 ; $i ++ ) { $collection -> insert ( array( 'i' => $i , "field { $i } " => $i * 2 ) );}
計算文檔數量:
echo $collection -> count ();
使用遊標擷取所有文檔
要活的集合中的所有文檔,我們需要 MongoCollection::find() 方法。 find() 方法返回一個 MongoCursor 對象,允許我們遍曆整個結果集合來讀取文檔。
$cursor = $collection -> find ();foreach ( $cursor as $id => $value ) { echo " $id : " ; var_dump ( $value );}
設定查詢條件
$query = array( 'i' => 71 );$cursor = $collection -> find ( $query );while ( $cursor -> hasNext () ) { var_dump ( $cursor -> getNext () );}
建立索引
$collection -> ensureIndex ( array( "i" => 1 ) ); // create index on "i"
$collection -> ensureIndex ( array( "i" => - 1 , "j" => 1 ) ); // index on "i" descending, "j" ascending
SQL 到 Mongo的對應表
SQL查詢語句 |
Mongo查詢語句 |
CREATE TABLE USERS (a Number, b Number) |
隱式的建立,或 MongoDB::createCollection() . |
INSERT INTO USERS VALUES(1,1) |
$db->users->insert(array("a" => 1, "b" => 1)); |
SELECT a,b FROM users |
$db->users->find(array(), array("a" => 1, "b" => 1)); |
SELECT * FROM users WHERE age=33 |
$db->users->find(array("age" => 33)); |
SELECT a,b FROM users WHERE age=33 |
$db->users->find(array("age" => 33), array("a" => 1, "b" => 1)); |
SELECT a,b FROM users WHERE age=33 ORDER BY name |
$db->users->find(array("age" => 33), array("a" => 1, "b" => 1))->sort(array("name" => 1)); |
SELECT * FROM users WHERE age>33 |
$db->users->find(array("age" => array('$gt' => 33))); |
SELECT * FROM users WHERE age<33 |
$db->users->find(array("age" => array('$lt' => 33))); |
SELECT * FROM users WHERE name LIKE "%Joe%" |
$db->users->find(array("name" => new MongoRegex("/Joe/"))); |
SELECT * FROM users WHERE name LIKE "Joe%" |
$db->users->find(array("name" => new MongoRegex("/^Joe/"))); |
SELECT * FROM users WHERE age>33 AND age<=40 |
$db->users->find(array("age" => array('$gt' => 33, '$lte' => 40))); |
SELECT * FROM users ORDER BY name DESC |
$db->users->find()->sort(array("name" => -1)); |
CREATE INDEX myindexname ON users(name) |
$db->users->ensureIndex(array("name" => 1)); |
CREATE INDEX myindexname ON users(name,ts DESC) |
$db->users->ensureIndex(array("name" => 1, "ts" => -1)); |
SELECT * FROM users WHERE a=1 and b='q' |
$db->users->find(array("a" => 1, "b" => "q")); |
SELECT * FROM users LIMIT 10 SKIP 20 |
$db->users->find()->limit(10)->skip(20); |
SELECT * FROM users WHERE a=1 or b=2 |
$db->users->find(array('$or' => array(array("a" => 1), array("b" => 2)))); |
SELECT * FROM users LIMIT 1 |
$db->users->find()->limit(1); |
EXPLAIN SELECT * FROM users WHERE z=3 |
$db->users->find(array("z" => 3))->explain() |
SELECT DISTINCT last_name FROM users |
$db->command(array("distinct" => "users", "key" => "last_name")); |
SELECT COUNT(*y) FROM users |
$db->users->count(); |
SELECT COUNT(*y) FROM users where AGE > 30 |
$db->users->find(array("age" => array('$gt' => 30)))->count(); |
SELECT COUNT(AGE) from users |
$db->users->find(array("age" => array('$exists' => true)))->count(); |
UPDATE users SET a=1 WHERE b='q' |
$db->users->update(array("b" => "q"), array('$set' => array("a" => 1))); |
UPDATE users SET a=a+2 WHERE b='q' |
$db->users->update(array("b" => "q"), array('$inc' => array("a" => 2))); |
DELETE FROM users WHERE z="abc" |
$db->users->remove(array("z" => "abc")); |