I. MongoDB Introduction
MongoDB (named from "humongous") is a scalable, high-performance, open-source, free-mode, document-oriented database, it combines the advantages of document databases, key-value pairs, and relational databases. Official site: http://www.mongodb.org/,mongodbhighlights:
• Document-oriented storage (simple and powerful JSON-like data mode)
• Dynamic query
• Full index support, extended to internal objects and embedded Arrays
• Query Record Analysis
• Fast and local updates
• Efficient storage of Binary large objects (such as photos and videos)
• Support for replication and Failover
• Auto-Sharding supports cloud-level scalability
• MapReduce supports complex Aggregation
• Business support, training and consulting
Ii. Install MongoDB
It is very easy to install MongoDB. You only need to download the compressed package and decompress the running command: http://www.mongodb.org/downloads. this document uses the Windows platform and mongodbruntime command:> bin/mongod. Tip: first, create a data storage folder. The default data storage directory of MongoDB is/data/db/(or c: \ data \ db). Of course, you can change it to a different directory, you only need to specify the -- dbpath parameter, for example:
> Bin/mongod -- dbpath = d: \ mgdata \ db
Iii. Install MongoDB PHP Extension
Download the PHP Extension: http://www.php.net/manual/en/#.installation.php?#.installation.windowsbased on your PHP version. The prompt is:
1. VC6 is suitable for Apache and VC9 for IIS;
2. Thread safe is suitable for running PHP modules and Non-thread safe is suitable for running CGI.
Modify php. ini, add: extension = php_assist.dll, and restart the Web server.
Iv. PHP example
1. Connect to the Mongo Server
Copy codeThe Code is as follows: <? Php
// Connect to localhost: 27017
$ Conn = new Mongo ();
// Connect to the default port of the remote host
$ Conn = new Mongo ('test. com ');
// Connect to port 22011 of the remote host
$ Conn = new Mongo ('test. com: 100 ');
// MongoDB has a user name and password
$ Conn = new Mongo ("mongodb: // $ {username }:: {password} @ localhost ");
// MongoDB has a username and password and specifies the Database blog
$ Conn = new Mongo ("mongodb: // $ {username }:: {password} @ localhost/blog ");
// Multiple servers
$ Conn = new Mongo ("mongodb: // localhost: 27017, localhost: 27018 ");
?>
2. Specify the database and dataset name (table name)Copy codeThe Code is as follows: <? Php
// Select the Database blog
$ Db = $ conn-> blog;
// Specify the result set (Table Name: users)
$ Collection = $ db-> users;
?>
3. CRUD
<? Php
// Add
$ User = array ('name' => 'caleng', 'email '=> 'admin @ admin.com ');
$ Collection-> insert ($ user );
// Modify
$ Newdata = array ('$ set' => array ("email" => "test@test.com "));
$ Collection-> update (array ("name" => "caleng"), $ newdata );
// Delete
$ Collection-> remove (array ('name' => 'caleng'), array ("justOne" => true ));
// Search
$ Cursor = $ collection-> find ();
Var_dump ($ cursor );
// Find one
$ User = $ collection-> findOne (array ('name' => 'caleng'), array ('email '));
Var_dump ($ user );
?>
4. Close the connection
<? Php
$ Conn-> close ();
?>