php+ MongoDB

Source: Internet
Author: User
Tags findone mongodb driver

Environment:

uname-v#1SMP Debian3.2. $-3+DEB7U2
PHP-vphp5.4. --1~dotdeb.1(CLI) (Built:jun in the at: -: -) Copyright (c)1997- theThe PHP groupzend Engine v2.4.0, Copyright (c)1998- theZend Technologies

To install the MongoDB driver:

1, install pecl (PHP extension Module)

sudo Install Php5-dev php5-cli php-pear

2, through the PECL installation drive

sudo Install MONGO

The installation process has a hint, the default carriage return.

Edit php.ini File:

sudo VI /etc/php5/fpm/php.ini

Add a line at the end of the file:

Extension=mongo.so

Finally, restart the PHP service:

Sudo

Test

Create a new PHP file: test.php, which reads as follows:

<?PHP$user=Array(    ' first_name ' = ' MongoDB ', ' last_name ' = ' Fan ', ' tags ' and ' = 'Array(' Developer ', ' user '));//Configuration$dbhost= ' localhost ';$dbname= ' Test ';//Connect to test database$m=NewMongo ("mongodb://$dbhost");$db=$m-$dbname;//Get the Users collection$c _users=$db-users;//Insert This new document into the Users collection$c _users->save ($user);?>

This is a record inserted in the Users table in the MongoDB test database. As MongoDB says, a document is inserted in the collection of database test users.

Open the test.php page in the browser and the data is inserted into the database. Go back to MongoDB to see if there is any data:

MONGO Testmongodb Shell version:2.6.3Connecting To:test>Db.users.findOne () {"_id": ObjectId ("53b79360b2c88865388b4567"),    "first_name":"MongoDB",    "last_name":"Fan",    "Tags" : [        "Developer",        "User"    ]}>

Yes, you can see that the code in PHP is already in effect.

How to read MongoDB data in PHP?

Create a new test2.php file with the following contents:

<?PHP//Configuration$dbhost= ' localhost ';$dbname= ' Test ';//Connect to test database$m=NewMongo ("mongodb://$dbhost");$db=$m-$dbname;//Get the Users collection$c _users=$db-users;//Find the user with first_name ' MongoDB ' and last_name ' Fan '$user=Array(    ' first_name ' = ' MongoDB ', ' last_name ' and ' Fan ');$user=$c _users->findone ($user);Var_dump($user);?>

Open test2.php in the browser to see the results.

Related Article

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.