Action
Before doing so, I hope you can take a look at the above learning resources and learn about the basic operations.
Initial account password for Data Connection
Account: admin
Password: admin
First, create a file: mongodb. php, set the account password to be connected, and connect to the database.
<?php/** * Mongodb Test * * Tools: * Zend Studio 8.x * Eclipse Plug :Toggle Varpper
** @ Author Wu Bai Qing <wbqyyicx@gmail.com> * @ version $ Id: Mongodb. php 17 06: 04: 15Z wbq $ */$ pai_server_name = 'admin'; $ pai_server_pwd = 'admin'; // connect to the Mongo database address: Port/account: password; $ mongo = new Mongo ('mongodb: // localhost: 27017/admin: admin'); // select a database and the set to be operated (if no database is created by default) $ collection = $ mongo-> selectDB ('rrs _ result')-> selectCollection ('content');?>
The database is automatically created.
Add a piece of data!
<? Php $ content = array ('title' => 'leaf-wu qingqing ', 'author' => 'wu qingqing', 'url' => 'HTTP: // www.cnblogs.com/wu?qing/archive/2011/09/17/2179870.html',); $ collection-> insert ($ content);?>
Stored data content:
Query all data:
<?php$rows = $collection->find();foreach ($rows as $key => $val){ var_dump($val);}?>
Result:
Change Data:
<? Php $ where = array ('title' => 'leaf-wu qingqing '); $ set = array ('title' => 'leaf '); $ collection-> update ($ where, array ('$ set' => $ set,);?>
Result:
Delete data:
<? Php $ collection-> remove (array ('title' => 'leaf ',);?>
Result:
No records is found.
Complete code:
<? Php/*** Mongodb Test ** Tools: * Zend Studio 8.x * Eclipse Pulg: Toggle Varpper ** @ author Wu Bai Qing <wbqyyicx@gmail.com> * @ version $ Id: Mongodb. php 17 06: 04: 15Z wbq $ */$ pai_server_name = 'admin'; $ pai_server_pwd = 'admin'; // connect to the Mongo database address: Port/account: password; $ mongo = new Mongo ('mongodb: // localhost: 27017/admin: admin'); // select a database and the set to be operated (if no database is created by default) $ collection = $ mongo-> selectDB ('rrs _ resu Lt ')-> selectCollection ('content');/* // Add $ content = array ('title' => 'leaf-Wu Xiaoqing ', 'author' => 'wu qingqing ', 'url' => 'HTTP: // www.cnblogs.com/wu?qing/archive/2011/09/17/2179870.html',); $ collection-> insert ($ content ); * // query $ colle = $ collection-> find (array ('title' => 'leaf-Wu Xiaoqing ')); foreach ($ colle as $ key => $ val) {var_dump ($ val );} * // modify $ where = array ('title' => 'leaf-Wu Xiaoqing '); $ set = array ('title' => 'leaf '); $ collection-> upd Ate ($ where, array ('$ set' => $ set ,)); * // Delete $ collection-> remove (array ('title' => 'leaf ',); * // End?>
The following are some syntaxes corresponding to SQL and Mongodb:
SQL Statement |
Mongo Query Language Statement |
Create table users (a Number, B Number) |
Implicit or use 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 |
$ 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 ("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 "));
|