windows下phpmongodb安裝配置使用查詢
這幾天參加了一個創意馬拉松大賽,雖然沒拿什麼獎,重在參與嘛
終於有機會實踐mongodb資料庫了,以前只是自己配置裝著玩玩
作者:風來了.呆狐狸
環境:window10 64 +php5.5.x+mysql5.7.x+mongodb2.6.x
mongod安裝
1.下載
http://www.mongodb.org/downloads
我這邊下載的是64-bit msi
2.安裝
預設就可以
預設安裝目錄
C:Program FilesMongoDB 2.6 Standardin
3.配置
為了省事另存為 d:mongodb.cnf
dbpath = d:mongodbdata bind_ip = 127.0.0.1 port = 27017 quiet = true logpath = d:mongodblogmongod.log logappend = true journal = true
4.啟動
這邊使用的是bat批處理啟動,省事。
下面就是 啟動.bat
C:"Program Files""MongoDB 2.6 Standard"inmongod.exe -f d:mongodb.cnf
如果要生成系統服務(不需要每次關閉電腦後還要重新啟動資料庫)請用
C:"Program Files""MongoDB 2.6 Standard"inmongod.exe --config d:mongodb.cnf --install
5.資料庫使用者名稱密碼
啟動成功後資料庫使用者名稱和密碼預設是空
php mongod 擴充套件安裝
下載:http://download.csdn.net/detail/dupingjin/7577217
根據PHP版本選擇相應的 擴充套件,這邊使用
php_mongo-1.4.5-5.5-vc11-nts.dll
放入php目錄ext資料夾下
修改 php.ini
在;extension 下一行或 php.ini末尾增加
extension=php_mongo-1.4.5-5.5-vc11-nts.dll
重新啟動PHP/NGINX/APACHE環境
測試輸出看 phpinfo 中是否有 mongo 這個環境引數,有則安裝成功!
測試
<?php header("Content-type:text/html;charset=utf-8"); $m = new MongoClient("mongodb://127.0.0.1:27017"); //如果patent資料庫不存在,預設自動新建 $db = $m->patent; //如果title表不存在,預設自動新建 $collection = $db->title; echo `<hr/>`; echo "查詢顯示結果"; echo `<hr/>`; /* $count = $collection->find()->count(); echo "總數:$count<br/>"; $cursor = $collection->find()->skip(0)->limit(5); $count = $cursor->count(true); echo "第一頁條數:$count<br/>"; foreach ($cursor as $document) { print_r($document); } */ //echo `<hr/>`; //echo "一條"; //echo `<hr/>`; //$cursor=$collection->findOne(); //print_r($cursor); //echo $collection->count();
php mongod 增刪改查詢
1.新增
$m = new MongoClient("mongodb://127.0.0.1:27017"); //如果lanmps資料庫不存在,預設自動新建 $db = $m->lanmps; //如果title表不存在,預設自動新建 $collection = $db->title; $add = [ "title" => "www.lanmps.com", "author" => "風來了" ]; $result=$collection->insert($add); //將$add 新增到$collection 集合中 echo "新記錄ID:".$add[`_id`]; #MongoDB會返回一個記錄標識 var_dump($result); #返回:bool(true)
2.修改更新
$m = new MongoClient("mongodb://127.0.0.1:27017"); //如果lanmps資料庫不存在,預設自動新建 $db = $m->lanmps; //如果title表不存在,預設自動新建 $collection = $db->title; $where = [ "title" => "test.lanmps.com", "author" => "風來了" ,"id"=>new MongoId(`3sdfasfzxcv234234sf`)]; $coll->update(["host" => "www.lanmps.com"], [`$set` => $where]);
3.刪除
$m = new MongoClient("mongodb://127.0.0.1:27017"); //如果lanmps資料庫不存在,預設自動新建 $db = $m->lanmps; //如果title表不存在,預設自動新建 $collection = $db->title; $where = [ "title" => "www.lanmps.com", "author" => "風來了" ,"id"=>new MongoId(`3sdfasfzxcv234234sf`)]; //刪除 $collection->remove($where);
4.查詢
$m = new MongoClient("mongodb://127.0.0.1:27017"); //如果lanmps資料庫不存在,預設自動新建 $db = $m->lanmps; //如果title表不存在,預設自動新建 $collection = $db->title; $where = [ "title" => "www.lanmps.com", "author" => "風來了" ,"id"=>new MongoId(`3sdfasfzxcv234234sf`)]; //查詢一條 $cursor=$collection->findOne($where,[`title`,`author`,`text`]); var_dump($cursor); //查詢 多條 $cursor = $collection->find($where); var_dump($cursor);
$m = new MongoClient("mongodb://127.0.0.1:27017"); //如果lanmps資料庫不存在,預設自動新建 $db = $m->lanmps; //如果title表不存在,預設自動新建 $collection = $db->title; $where = [ "title" => "www.lanmps.com", "author" => "風來了" ,"id"=>new MongoId(`3sdfasfzxcv234234sf`)]; /** 查詢記錄數 **/ echo $collection->count(); #全部 echo `<br/>`; echo $collection->count($where); #可以加上條件 echo `<br/>`; echo $collection->count([`day`=>[`$gt`=>10,`$lte`=>20]]); #大於10小於等於20 echo `<br/>`; //limit 顯示5條,從第0條開始 echo $collection->find()->limit(5)->skip(0)->count(true); #獲得實際返回的結果數 // 注:$gt為大於、$gte為大於等於、$lt為小於、$lte為小於等於、$ne為不等於、$exists不存在
模糊查詢
$querys = ["name" => new MongoRegex("/*.asdfsf*./$i")]; $collection->find($querys);
相關文章
- Windows下Scoop安裝、配置與使用WindowsOOP
- windows下Tomcat安裝配置WindowsTomcat
- windows下安裝cygwin及配置Windows
- windows下配置安裝YAF環境Windows
- windows下mangodb的安裝及配置WindowsGo
- Mongodb在Windows下安裝及配置MongoDBWindows
- windows下Emacs的安裝與配置WindowsMac
- Windows10 下caffe-Windows安裝與配置Windows
- solr在windows下的安裝及配置SolrWindows
- windows下PHP與Apache的安裝配置WindowsPHPApache
- windows下安裝配置 Navisphere Host AgentWindows
- git安裝及配置教程 windows windows上git的安裝和使用GitWindows
- CodeMan快速安裝配置教程:Windows下安裝配置Java JDK環境WindowsJavaJDK
- CentOS下tftp 安裝配置使用CentOSFTP
- windows下Redis的安裝和使用WindowsRedis
- windows下安裝MongoDB擴充套件和配置WindowsMongoDB套件
- (轉)Windows下安裝Docker, GitBash環境配置WindowsDockerGit
- Windows下 MySQL-5.7.11 安裝與配置WindowsMySql
- windows系統下GCC的安裝與配置WindowsGC
- Tomcat在Windows下的免安裝配置TomcatWindows
- Windows下PHP和Apache的安裝與配置WindowsPHPApache
- windows 下安裝Windows
- mac/linux查詢軟體安裝、配置路徑MacLinux
- ssh查詢及其安裝
- Windows10下如何安裝配置 perl 環境Windows
- windows下安裝jdk+tomcat+maven並配置WindowsJDKTomcatMaven
- Apache+php在windows下的安裝和配置ApachePHPWindows
- Windows環境下phpMyAdmin的安裝配置方法WindowsPHP
- windows下nvm、nrm的安裝及使用Windows
- windows上安裝配置redisWindowsRedis
- Robot Framework安裝配置 windowsFrameworkWindows
- windows 下安裝 nvmWindows
- windows 下安裝 yarnWindowsYarn
- Windows下安裝MongoDBWindowsMongoDB
- windows下安裝MySQLWindowsMySql
- Windows 下安裝GNUstepWindows
- windows下安裝glassfishWindows
- windows下檢查是否安裝了JDK以及檢視JDK安裝路徑WindowsJDK