Windows PHP MongoDB installation configuration using queries

Source: Internet
Author: User
Tags findone mongoclient php mongodb
In the past few days to participate in a creative marathon, although did not take any prize, the heavy participation

Finally have the opportunity to practice the MongoDB database, used to just own configuration to play

Author: The wind is coming. The Fox

Environment: WINDOW10 +php5.5.x+mysql5.7.x+mongodb2.6.x

Mongod Installation

1. Download

Http://www.mongodb.org/downloads

I'm downloading the 64-bit msi here.

2. Installation

By default, you can

Default installation directory

C:\Program FILES\MONGODB 2.6 Standard\bin

3. Configuration

Save for D:\MONGODB.CNF

DBPath = D:\mongodb\databind_ip = 127.0.0.1port = 27017quiet = Truelogpath = D:\mongodb\log\mongod.loglogappend = Truejour NAL = True

4. Start

This is the use of Bat batch start, easy.
Here's the boot. bat

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

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

C \ "program Files" \ "MongoDB 2.6 standard" \bin\mongod.exe--config d:\mongodb.cnf--install

5. Database User name password

Database user name and password are empty by default after successful startup

PHP Mongod Extended Installation

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

Choose 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

Extension the end of a line or php.ini.

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

Test

 
  patent;//Default Auto New $collection = $db->title;echo ' If the title table does not exist
 
  '; Echo ' query displays the result "; echo"
 
  '; * * $count = $collection->find ()->count (); echo "Total: $count
"; $cursor = $collection->find ()->skip (0)->limit (5); $count = $cursor->count (true); echo" Number of first pages: $count
"; foreach ($cursor as $document) { print_r ($document);} *///echo ' ';//echo ' one ";//echo" ';//$cursor = $collection->findone ();//print_r ($cursor);//echo $collection->count ();

PHP mongod Delete and change query

1. Add

$m = new Mongoclient ("mongodb://127.0.0.1:27017");//If the Lanmps database does not exist, the default auto new $db = $m->lanmps;//If the title table does not exist, the default auto new $ Collection = $db->title; $add = ["title" = "Www.lanmps.com", "Author" = "Wind"]; $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 auto new $db = $m->lanmps;//If the title table does not exist, the default auto new $ Collection = $db->title; $where = ["title" = "Test.lanmps.com", "Author" = "Wind", "id" =>new MongoId (' 3sdfasf ZXCV234234SF ')]; $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 auto new $db = $m->lanmps;//If the title table does not exist, the default auto new $ Collection = $db->title; $where = ["title" = "Www.lanmps.com", "Author" = "Wind", "id" =>new MongoId (' 3sdfasfz XCV234234SF ')];//delete $collection->remove ($where);

4. Enquiry

$m = new Mongoclient ("mongodb://127.0.0.1:27017");//If the Lanmps database does not exist, the default auto new $db = $m->lanmps;//If the title table does not exist, the default auto new $ Collection = $db->title; $where = ["title" = "Www.lanmps.com", "Author" = "Wind", "id" =>new MongoId (' 3sdfasfz XCV234234SF ')];//find a $cursor= $collection->findone ($where, [' title ', ' Author ', ' text ']); Var_dump ($cursor);//Find Multiple $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 auto new $db = $m->lanmps;//If the title table does not exist, the default auto new $ Collection = $db->title; $where = ["title" = "Www.lanmps.com", "Author" = "Wind", "id" =>new MongoId (' 3sdfasfz XCV234234SF ')];/** query Record Count **/echo $collection->count (); #全部echo '
'; Echo $collection->count ($where); #可以加上条件echo '
'; Echo $collection->count ([' Day ' =>[' $gt ' =>10, ' $lte ' =>20]]); #大于10小于等于20echo '
';//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 or equal, $ne is not equal to, $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.