mongodb php
2016-02-16 10:52
211 查看
首先安装扩展,然后才能使用mongodb
一、连接数据库
该代码可以连接mydb数据库,如果该数据库不存在则自动创建。
二、创建集合
该代码可以创建集合mycol。
三、插入文档
mongodb中使用insert()来插入文档。
输出:
四、查找文档
mongodb使用find()来查找文档
结果:
五、更新文档
使用update()来更新文档
结果
六、删除文档
remove方法
options删除的选项:
“w”:抛出异常的级别,默认是1;
“justOne”:最多只删除一个匹配的记录;
“fsync”:Boolean, defaults to
“timeout”:Integer, defaults to MongoCursor::$timeout. If "safe" is set, this sets how long (in milliseconds) for the client to wait for a database response. If the database does not respond within the timeout period, aMongoCursorTimeoutException will be thrown.
......
其他方法可参见php手册:http://php.net/manual/zh/book.mongo.php
一、连接数据库
try { $mongo = new MongoClient(); $db = $mongo->mydb; var_dump($db); } catch (MongoConnectionException $e) { echo $e->getMessage(); }
该代码可以连接mydb数据库,如果该数据库不存在则自动创建。
二、创建集合
try { $mongo = new MongoClient(); $db = $mongo->mydb; $mycol = $db->createCollection('mycol'); var_dump($mycol); } catch (MongoConnectionException $e) { echo $e->getMessage(); }
该代码可以创建集合mycol。
三、插入文档
mongodb中使用insert()来插入文档。
try { $mongo = new MongoClient(); $db = $mongo->mydb; $mycol = $db->mycol; $document = array('name' => 'test1' , 'sex' => 'formale' , 'age' => 20); $res = $mycol->insert($document); var_dump($res); } catch (MongoConnectionException $e) { echo $e->getMessage(); }
输出:
array (size=4) 'ok' => float 1 'n' => int 0 'err' => null 'errmsg' => null
四、查找文档
mongodb使用find()来查找文档
try { $mongo = new MongoClient(); $db = $mongo->mydb; $mycol = $db->mycol; $mongoCursor = $mycol->find(); foreach ($mongoCursor as $document) { var_dump($document); } } catch (MongoConnectionException $e) { echo $e->getMessage(); }
结果:
array (size=4) '_id' => object(MongoId)[7] public '$id' => string '56c28a793b22cf5415000029' (length=24) 'name' => string 'test1' (length=5) 'sex' => string 'formale' (length=7) 'age' => int 20
五、更新文档
使用update()来更新文档
try { $mongo = new MongoClient(); $db = $mongo->mydb; $mycol = $db->mycol; $mycol->update(array('name'=>'test1') , array('$set'=>array('age' => 21))); $mongoCursor = $mycol->find(); foreach ($mongoCursor as $document) { var_dump($document); } } catch (MongoConnectionException $e) { echo $e->getMessage(); }
结果
array (size=4) '_id' => object(MongoId)[7] public '$id' => string '56c28a793b22cf5415000029' (length=24) 'name' => string 'test1' (length=5) 'sex' => string 'formale' (length=7) 'age' => int 21
六、删除文档
try { $mongo = new MongoClient(); $db = $mongo->mydb; $mycol = $db->mycol; $mycol->remove(array('name'=>'test1')); $mongoCursor = $mycol->find(); foreach ($mongoCursor as $document) { var_dump($document); } } catch (MongoConnectionException $e) { echo $e->getMessage(); }
remove方法
public bool|array MongoCollection::remove ([ array $criteria = array() [, array $options = array() ]] )
options删除的选项:
“w”:抛出异常的级别,默认是1;
“justOne”:最多只删除一个匹配的记录;
“fsync”:Boolean, defaults to
FALSE. Forces the insert to be synced to disk before returning success. If
TRUE, an acknowledged insert is implied and will override setting w to 0.
“timeout”:Integer, defaults to MongoCursor::$timeout. If "safe" is set, this sets how long (in milliseconds) for the client to wait for a database response. If the database does not respond within the timeout period, aMongoCursorTimeoutException will be thrown.
......
其他方法可参见php手册:http://php.net/manual/zh/book.mongo.php
相关文章推荐
- springMVC集成mongoDB的demo
- SpringMVC整合MongoDB开发 高级操作
- MongoDB一般安装
- mongodb shell警告
- 《MongoDB 权威指南》 学习总结
- mac安装mongodb
- MongoDB简单测试
- mongodb分片环境搭建启动mongos报错
- MongoDB 3.0 速上手教程(JAVA)
- JAVA单例MongoDB工具类
- mongodb_修改器($inc/$set/$unset/$push/$pop/upsert......)
- 【php + mongodb】windows php5.4 配置 mongodb 扩展(附各种其它版本下载地址)
- 【php + mongodb】windows php5.4 配置 mongodb 扩展(附各种其它版本下载地址)
- 搭建mongodb副本集群,keyfile和ssl方式,用户认证模式
- mongodb-$type、limit、skip、sort方法、索引、聚合
- MongoDB 3.0 for java 学习记录(一、java连接MongoDB)
- mongoDB 3.0 安全权限访问控制
- mongodb java driver3.0初探
- mongodb简介与增删该查
- mongodb的sum操作以及javaapi的实现