PHP操作mongoDB 笔记
连接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->insert(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 | $db->users->find(array("age" => 33), array("a" => 1, "b" => 1))->sort(array("name" => 1)); |
SELECT a,b FROM users WHERE age=33 | $db->users->find(array("age" => array('$gt' => 33))); |
SELECT a,b 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->ensureIndex(array("name" => 1)); |
SELECT * FROM users ORDER BY name DESC | $db->users->ensureIndex(array("name" => 1)); |
SELECT * FROM users ORDER BY name DESC | $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(array('$or' => array(array("a" => 1), array("b" => 2)))); |
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(array("z" => 3))->explain() |
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->update(array("b" => "q"), array('$inc' => array("a" => 2))); |
UPDATE users SET a=1 WHERE b='q' | $db->users->update(array("b" => "q"), array('$inc' => array("a" => 2))); |
UPDATE users SET a=1 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")); |