PHP operations on MongoDB [NoSQL] Databases

Source: Internet
Author: User
Tags install mongodb php example

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 ();
?>

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.