PHP implements basic operations on MongoDB

Source: Internet
Author: User
PHP extension PHP5.2, PHP5.3, thread, non-thread, IIS, Apache integrated: pan. baidu. coms1sjrW6z7 downloads the corresponding php extension file php_assist.dll, imports phpext into the php extension folder, and modifies php. the INI file adds the following lines: extensionphp_mongo.dllPHP to connect to the database $ c

PHP extension PHP5.2, PHP5.3, thread, non-thread, IIS, Apache integrated: http://pan.baidu.com/s/1sjrW6z7 download the corresponding php extension file php_mongo.dll, into the php extension Folder/php/ext, modify php. INI file, add the following lines: extension = php_assist.dll PHP operation to connect to database $ c

PHP Extension

PHP5.2, PHP5.3, thread, non-thread, IIS, and Apache:

Http://pan.baidu.com/s/1sjrW6z7

Download the corresponding php extension file php_mongo.dll, and paste it into the php extension Folder/php/ext. Modify the php. ini file and add the following lines:

Extension = php_cmd.dll

PHPOperation

  • Connect to database

$conn = new Mongo("mongodb://localhost:27017//admin:admin");

  • Select database and set

// Select the Database blog. If no, create $ db = $ conn-> yyd; // You can also write it as $ db = $ conn-> selectDB ('yyd '); // set the result set (set: yyd_test) $ collection = $ db-> yyd_test; // You can also write it as follows: $ collection = $ db-> selectCollection ('yyd _ test'); // var_dump ($ collection );

  • Add data

$post = array('name' => '22', 'sex' => '32');$flag=($collection->insert($post));var_dump($flag);

  • Search for Data

$arr=array();$cursor = $collection->find($arr);foreach($cursor as $key => $value){    echo "
";    echo $value['_id'];    echo '
name:'; echo $value['name']; echo "
sex:"; echo $value['sex']; echo "
";}

  • Conditional search

$arr=array("name"=>"22");$cursor = $collection->find($arr);

  • Modify data

$newdata = array('$set' => array("email" => "test@test.com"));$collection->update(array("name" => "22"), $newdata);var_dump($collection);$arr=array("name"=>"22");$cursor = $collection->find($arr);foreach($cursor as $key => $value){    echo "
";
    echo $value['_id'];    echo '
name:'; echo $value['name']; echo "
sex:"; echo $value['sex']; echo "
email:"; echo $value['email']; echo "
";}

  • Delete data

$arr=array("name"=>"22");$collection->remove($arr);var_dump($collection);

  • Other commonOperation

// Close the connection $ conn-> close (); // delete a database $ conn-> dropDB ("yyd "); // list all available databases $ dbs = $ conn-> listDBs ();

With MongoDBOperationFunction PHP manual:

Http://pan.baidu.com/s/1pJz2llh

Or Website:

Http://www.php.net/manual/zh/class.mongodb.php

Http://www.cnblogs.com/yydcdut/p/3571430.html

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.