PHP operating MongoDB [NoSQL] _ PHP Tutorial

Source: Internet
Author: User
Tags findone install mongodb php example
[Switch] PHP to operate MongoDB [NoSQL ]. Original: blog.sina.com.cnsblog_4b67d3240101519b.html I. MongoDB introduction MongoDB (name from humongous) is a scalable, high-performance, open-source, free mode, for the original: http://blog.sina.com.cn/s/blog_4b67d3240101519b.html

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://github.com/mongodb/developer-php-driver/downloadsbased on your PHP version. the following prompt is displayed:
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
View plaincopy to clipboardprint?
// 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 ");
?>
// 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)
View plaincopy to clipboardprint?
// Select the database blog
$ Db = $ conn-> blog;
// Specify the result set (table name: users)

$ Collection = $ db-> users;
?>
// Select the database blog
$ Db = $ conn-> blog;
// Specify the result set (table name: users)
$ Collection = $ db-> users;
?>
3. CRUD
View plaincopy to clipboardprint?
// 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 );
?>
// 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
View plaincopy to clipboardprint?
$ Conn-> close ();
?>

MongoDB (named from "humongous") is a scalable, high-performance, open-source, free-of-use mode, and oriented...

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.