PHP operations on MongoDB [NoSQL] databases. I. MongoDB introduction MongoDB (named from humongous) is a scalable, high-performance, open-source, free-mode, document-oriented database, set document database, key-value pair storage and off 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
The code is as follows:
// 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)
The code is as follows:
// Select the database blog
$ Db = $ conn-> blog;
// Specify the result set (table name: users)
$ Collection = $ db-> users;
?>
3. CRUD
// 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
$ Conn-> close ();
?>
Mongomongodb (named from "humongous") is a scalable, high-performance, open-source, free-mode, document-oriented database that collects document databases, key-value pairs for storage and off...