大佬教程收集整理的这篇文章主要介绍了mongodb php,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
一、连接数据库
try { $mongo = new MongoClIEnt(); $db = $mongo->@H_515_10@mydb; var_dump($db); } catch (Mongo@R_301_785@ $e) { echo $e->getmessage(); }@H_502_43@
该代码可以连接mydb数据库,如果该数据库不存在则自动创建。
二、创建集合
@H_515_10@mydb; $mycol = $db->createCollection('mycol'); $mycolgetmessage(); }@H_502_43@
该代码可以创建集合mycol。
三、插入文档
@H_973_0@mongodb中使用insert()来插入文档。$db->@H_515_10@mycol; $document = array('name' => 'test1','sex' => 'formale','age' => 20); $res = $mycol->insert($document$resgetmessage(); }@H_502_43@
输出:
array (size=4) 'ok' => float 1 'n' => int 0 'err' => null 'errmsg' => null@H_502_43@
四、查找文档
@H_973_0@mongodb使用find()来查找文档$mongocursor = $mycol->find(); foreach ($mongocursor as ) { ); } } getmessage(); }@H_502_43@
结果:
) '_ID' => object(MongoID)[7] public '$ID' => String '56c28a793b22cf5415000029' (length=24) 'name' => String 'test1' (length=5) 'sex' => String 'formale' (length=7) 'age' => int 20@H_502_43@
五、更新文档
使用update()来更新文档
$mycol->update(array('name'=>'test1'),array('$set'=>array('age' => 21))); getmessage(); }@H_502_43@
结果
)
'age' => int 21
@H_502_43@
六、删除文档
$mycol->remove(array('name'=>'test1')); getmessage(); }@H_502_43@
remove方法
public bool|array MongoCollection::remove ([ array $criteria = array() [,1)">$options = array() ]] )@H_502_43@
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 @H_923_229@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.
......
以上是大佬教程为你收集整理的mongodb php全部内容,希望文章能够帮你解决mongodb php所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。