PHP操作mongoDB 筆記
阿新 • • 發佈:2018-12-24
連線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) | |
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")); |