Getting started with Mongodb [CURD]-PHP

Source: Internet
Author: User
Before taking this action, I hope you can take a look at the above learning resources and learn about the basic operations. data Connection initial account password account: admin Password: admin first we create a file: mongodb. php, set the account password to be connected to the database? Php *** MongodbTest ** Tools: * ZendStudio8.x * EclipsePlug

Before taking this action, I hope you can take a look at the above learning resources and learn about the basic operations. data Connection initial account password account: admin Password: admin first we create a file: mongodb. php, set the account password to be connected to the database? Php/*** Mongodb Test ** Tools: * Zend Studio 8.x * Eclipse Plug

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.

 
** @ Author Wu Bai Qing
  
   
* @ 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!

  '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:



<喎?http: www.2cto.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + CrLp0a/L + dPQyv2 + 3To8L3A + Cgo8aW1nIHNyYz0 = "http://www.68idc.cn/help/uploads/allimg/150619/0514502151-0.gif" alt = "">

  find();foreach ($rows as $key => $val){    var_dump($val);}?>

Result:


Change Data:

  'Leaf-wu qingqing '); $ set = array ('title' => 'leaf'); $ collection-> update ($ where, array ('$ set' => $ set,);?>

Result:




Delete data:

  Remove (array ('title' => 'leaf ',);?>

Result:

No records is found.


Complete code:

  * @ 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 '); /* // Add $ content = array ('title' => 'leaf-wu qingqing ', 'author' => 'wu qingqing ', 'url' => 'HTTP: // www.cnblogs.com/ Wuw.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-> update ($ 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 "));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.