Windows PHP MongoDB installation configuration use Query

Source: Internet
Author: User
Tags findone mongoclient mongodb php mongodb

The last few days to participate in a creative marathon, although not take any prize, heavy participation

Finally have the opportunity to practice MongoDB database, used to only configure themselves to play

The wind is coming. Stay Fox

Environment: WINDOW10 +php5.5.x+mysql5.7.x+mongodb2.6.x mongod installation 1. Download

Http://www.mongodb.org/downloads

I'm here to download 64-bit msi 2. Installation

By default, you can

Default installation directory

C:\Program FILES\MONGODB 2.6 Standard\bin
3. Configure

Save the d:\mongodb.cnf for the sake of convenience

DBPath = d:\mongodb\data
bind_ip = 127.0.0.1
port = 27017 quiet
= True
LogPath = d:\mongodb\log\ Mongod.log
logappend = True
Journal = True
4. Start

This side uses the bat batch to start, the convenience.
Here's the boot. bat

C:\ "program Files" \ "MongoDB 2.6 Standard" \bin\mongod.exe-f d:\mongodb.cnf

If you want to generate system services (you do not need to restart the database every time you shut down the computer), use

C:\ "program Files" \ "MongoDB 2.6 Standard" \bin\mongod.exe--config, d:\mongodb.cnf--install
5. Database username Password

The database username and password are empty by default after successful startup
PHP Mongod Extended installation

Download: http://download.csdn.net/detail/dupingjin/7577217

Select the appropriate extension based on the PHP version, which is used here

Php_mongo-1.4.5-5.5-vc11-nts.dll
Into PHPDirectory extfolder under
Modify PHP.ini

To increase at the end of a line or php.ini extension.

Extension=php_mongo-1.4.5-5.5-vc11-nts.dll
Restart the Php/nginx/apache environment
Test output to see if there is MONGO this environment parameter in the Phpinfo, the installation is successful.

Test

<?php
Header ("Content-type:text/html;charset=utf-8");
$m = new Mongoclient ("mongodb://127.0.0.1:27017");
If the patent database does not exist, the default automatic new
$db = $m->patent;
If the title table does not exist, the default automatic new
$collection = $db->title;
Echo ' 
php mongod Additions and deletions change query

1. Add

$m = new Mongoclient ("mongodb://127.0.0.1:27017");
If the Lanmps database does not exist, the default automatic new
$db = $m->lanmps;
If the title table does not exist, the default automatic new
$collection = $db->title;
$add = ["title" => "Www.lanmps.com", "Author" => "The Wind came"];
$result = $collection->insert ($add);     Add $add to the $collection collection
echo "New record ID:". $add [' _id ']; #MongoDB会返回一个记录标识
var_dump ($result); #返回: bool (TRUE)
2. Modify the update
$m = new Mongoclient ("mongodb://127.0.0.1:27017");
If the Lanmps database does not exist, the default automatic new
$db = $m->lanmps;
If the title table does not exist, the default automatic new
$collection = $db->title;
$where = ["title" => "Test.lanmps.com", "Author" => "Wind", "id" =>new mongoid (' 3sdfasfzxcv234234sf ')];
$coll->update (["Host" => "www.lanmps.com"], [' $set ' => $where]);
3. Delete
$m = new Mongoclient ("mongodb://127.0.0.1:27017");
If the Lanmps database does not exist, the default automatic new
$db = $m->lanmps;
If the title table does not exist, the default automatic new
$collection = $db->title;
$where = ["title" => "Www.lanmps.com", "Author" => "Wind", "id" =>new mongoid (' 3sdfasfzxcv234234sf ')];
Delete
$collection->remove ($where);
4. Query
$m = new Mongoclient ("mongodb://127.0.0.1:27017");
If the Lanmps database does not exist, the default automatic new
$db = $m->lanmps;
If the title table does not exist, the default automatic new
$collection = $db->title;
$where = ["title" => "Www.lanmps.com", "Author" => "Wind", "id" =>new mongoid (' 3sdfasfzxcv234234sf ')];
Find a
$cursor = $collection->findone ($where, [' title ', ' Author ', ' text ']);
Var_dump ($cursor);
Find more than one
$cursor = $collection->find ($where);
Var_dump ($cursor);
$m = new Mongoclient ("mongodb://127.0.0.1:27017");
If the Lanmps database does not exist, the default automatic new
$db = $m->lanmps;
If the title table does not exist, the default automatic new
$collection = $db->title;
$where = ["title" => "Www.lanmps.com", "Author" => "Wind", "id" =>new mongoid (' 3sdfasfzxcv234234sf ')];
/** Query record number **/
echo $collection->count (); #全部
echo ' <br/> ';
echo $collection->count ($where); #可以加上条件
echo ' <br/> ';
echo $collection->count ([' Day ' =>[' $gt ' =>10 ', ' $lte ' =>20]]); #大于10小于等于20
echo ' <br/> ';
Limit shows 5, starting from No. 0
Echo $collection->find ()->limit (5)->skip (0)->count (true); #获得实际返回的结果数
/ /Note: $GT is greater than, $GTE is greater than or equal to, $lt is less than, $lte is less than equal, $ne is not equal, $exists does not exist
Fuzzy query
$querys = ["Name" => new Mongoregex ("/*.asdfsf*./$i")];
$collection->find ($querys);


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.