1,查看官方介绍http://www.mongodb.org/display/DOCS/PHP+Language+Center
2,下载驱动程序http://www.mongodb.org/downloads
linux:
安装pear:sudo apt-get install php5-dev php5-cli php-pear
安装扩展:sudo pecl install mongo
启用扩展:php.ini 中加入 extension=mongo.so
win:https://github.com/mongodb/mongo-php-driver/downloads
安装扩展:从压缩包中取出php_mongo.dll(php_mongo-1.3.0-5.2-vc9.dll),放入php的ext目录
启用扩展:php.ini 中加入 extension=php_mongo.dll
3,重启apache
4,常用操作
- $db = new MongoClient( "mongodb://example.com:65432" );//连接
- $db = $m->test; //选择一个数据库
- $table = $db->t; //选择一个集合(表)
- $table->insert(array("title"=>"1111", "author"=>"lyj"));//插入记录
- $table->ensureIndex( array( "i" => 1 ) );//为t表i字段加入正序(升序)索引
- $one = $table->findOne();// 查询一条记录
- $cursor = $table->find(array('i'=>array('$lte'=>5)));//i<=5
- foreach($cursor as $vo){//循环输出
- foreach($vo as $k=>$v){
- echo $k.':'.$v.', ';
- }
- echo '<Br>';
- }
- $table->count(); //记录数
转载于:https://blog.51cto.com/liyongjiang/1129666