Home  >  Article  >  Backend Development  >  [Transfer] PHP operation MongoDB [NoSQL]_PHP tutorial

[Transfer] PHP operation MongoDB [NoSQL]_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:13:21770browse

Original text: http://blog.sina.com.cn/s/blog_4b67d3240101519b.html

1. Introduction to MongoDB

MongoDB (the name comes from "humongous") is a scalable, high-performance, open source, schema-free, document-oriented database that combines the advantages of document databases, key-value storage and relational databases. Official site: http://www.mongodb.org/, MongoDB features:

• Document-oriented storage (JSON-like data schema is simple and powerful)
• Dynamic query
• Full index support, extended to internal objects and Embedded array
• Query record analysis
• Fast, in-place updates
• Efficient storage of binary large objects (such as photos and Video)
• Replication and failover support
•Auto-Sharding supports cloud-scale scalability
•MapReduce support Complex aggregation
•Commercial support, training and consulting
2. Install MongoDB

Installing MongoDB is very simple, just download the compressed package, decompress it and run the command. Download address: http://www.mongodb.org/downloads, This article is for the windows platform. MongoDB runs the command: >bin/mongod. Tip: First create a folder to store data. MongoDB’s default data storage directory is /data/db/ (or c:datadb). Of course, you can also change it to a different directory by specifying the --dbpath parameter, eg:
>bin/mongod --dbpath=d:mgdatadb
3. Install the MongoDB PHP extension
Download PHP according to your own PHP version Extension: http://github.com/mongodb/mongo-php-driver/downloads, Tips:
1. VC6 is suitable for Apache, VC9 Suitable for IIS;
2. Thread safe is suitable for PHP running in module mode, and Non-thread safe is suitable for CGI running mode.
Modify php.ini, add: extension=php_mongo.dll, and restart the web server.
4. PHP example
1. Connect to Mongo server
view plaincopy to clipboardprint?

//Connect to localhost:27017
$conn = new Mongo();
//Connect to the remote host Default port
$conn = new Mongo('test.com');
//Connect to remote host 22011 port
$ conn = new Mongo('test.com:22011');
//MongoDB has username 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 localhost:27017
$conn = new Mongo();
//Connect to the remote host default port
$conn = new Mongo ('test.com');
//Connect to the remote host port 22011
$conn = new Mongo('test.com:22011');
//MongoDB has username 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 database and data set name (table name)
view plaincopy to clipboardprint?

//Select database blog
$db = $conn->blog;
/ /Develop result set (table name: users)

$collection = $db->users;
?>

//Select the database blog
$db = $conn->blog;
//Develop 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));
//Find
$cursor = $collection->find();
var_dump($cursor);
//Find one
$user = $collection->findOne(array('name' => 'caleng'), array('email'));
var_dump($user);
?>

//New 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));
//Find
$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();
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440374.htmlTechArticleOriginal text: http://blog.sina.com.cn/s/blog_4b67d3240101519b.html 1. Introduction to MongoDB MongoDB (name comes from "humongous") is an extensible, high-performance, open source, model-free, oriented...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn