mognodb数据库连接.
默认格式
$m = new mongo();//这里采用默认连接本机的27017端口,当然也可以连接远程主机如 192.168.0.4:27017,如果端口是27017,端口可以省略。
标准连接
$m = new mongo("mongodb://${urname}:${password}@localhost");
实例:
$m = new mongo("mongodb://127.0.0.1:27017/admin:admin");
数据库的用户名和密码都是admin
数据库操作
插入数据
<?php//这里采用默认连接本机的27017端口,当然你也可以连接远程主机如192.168.0.4:27017//如果端口是27017,端口可以省略$m = new mongo("mongodb://127.0.0.1:27017/admin:admin");//选择comedy数据库,如果以前没该数据库会自动创建,也可以用$m->lectdb("comedy");$db = $m->comedy;//选择comedy里面的collection集合,相当于rdbms里面的表,也可以使用$collection = $db->collection;$db->lectcollection("collection");/*********添加一个元素**************/$obj = array("title" => "php1", "author" => "bill watterson");//将$obj 添加到$collection 集合中$collection->inrt($obj);/*********添加另一个元素**************/$obj = array("title" => "huaibei", "online" => true);$collection->inrt($obj);//$query = array("title" => "huaibei");$query = array( "_id" => $obj['_id'] );$cursor = $collection->find($query);//遍历所有集合中的文档foreach ($cursor as $obj) { echo $obj["title"] . "\n"; echo $obj["_id"] . "\n";}//断开mongodb连接$m->clo();
带条件的查询
mysql: id = 123mongo: array(‘id'=>123)mysql: name link '%bar%'mongo: array(‘name' => new mongoregex(‘/.*bar.*/i'))mysql: where id > 10mongo: array(‘id' => array(‘$gt' => 10))mysql: where id >= 10mongo: array(‘id' => array(‘$gte' => 10))mysql: where id < 10mongo: arr雪梨花ay(‘id' => array(‘$lt' => 10))mysql: where id <= 10mongo: array(‘id' => array(‘$lte' => 10))mysql: where id > 1 and id < 10mongo: array(‘id' => array(‘$gt' => 1,'$lt' => 10))mysql: where id <> 10mongo: array(‘id' => array(‘$ne' => 10))mysql: where id in(123)mongo: array(‘id' => array(‘$in' => array(1,2,3)))mysql: where id not in(123)mongo: array(‘id' => array(‘$nin' => array(1,2,3)))mysql: where id = 2 or id = 9mongo: array(‘id' => array(‘$or' => array(array(‘id'=>2),array(‘id'=>9))))mysql: order by name ascmongo: array(‘sort'=>array(‘name'=>1))mysql: order by name descmongo: array(‘sort'=>array(‘name'=>-1))mysql: limit 0,2mongo: array(‘limit'=>array(‘offt'=>0,'rows'=>2))mysql: lect name,emailmongo: array(‘name','email')mysql: lect count(name)mongo: arr一衣带水的水是指ay(‘count') //注意:count为大写
查询时,每个object插入时都会自动生成一个独特的_id,它相当于rdbms中的主键,用于查询时非常方便 (_id每一都不同,很像自动增加的id)
<?php$param = array("name" => "joe");$collection->inrt($param);$joe = $collection->findone(array("_id" => $param['_id']));print_r($joe);$m->clo();
返回结果:array ( [_id] => mongoid object ( [$id] => 4fd30e21870da83416000002 ) [name] => joe )
更改字段值
<?php$sign = array("title" => 'php1');$param = array("title" => 'php1','author'=>'test');$joe = $collection->update($sign, $param);
删除一个数据库
$m -> dropdb(“comedy”);
列出所有可用数据库
$m->listdbs(); //无返回值
创建一个mongodb对象
<?php$mo = new mongo();$db = new mongodb($mo,'dbname');//通过创建方式获得一个mongodb对象
删除当前db
<?php$db = $mo->dbname;$db->drop();
获得当前数据库名
<?php$db = $mo->dbname;$db->_tostring();
选择想要的collection:
//a:$mo = new mongo();$coll = $mo->dbname->collname;//获得一个collection对象//b:$db = $mo->lectdb('dbname');$coll = $db->collname;//c:$db = $mo->dbname;$coll = $db->collname;//d:$db = $mo->dbname;$coll = $db->lectcollectoin('collname');//获得一个collection对象
插入数据(mongocollection对象
$coll = $mo->db->foo;$a = array('a'=>'b');$options = array('safe'=>true);$rs =$coll->inrt($a,$options);
删除数据库中的记录(mongocollection对象)
$coll = $mo->db->coll;$c = array('a'=>1,'s'=>array('$lt'=>100));$options = array('safe'=>true);$rs = $coll->remove($c,$options);
更新数据库中的记录(mongocollection对象)
$coll = $mo->db->coll;$c = array('a'=>1,'s'=>array('$lt'=>100));$newobj = array('e'=>'f','x'=>'y');$options = array('safe'=>true,'multiple'=>true);$rs = $coll->remove($c,$newobj,$options);
查询collection获得单条记录(mongocollection类)
$coll = $mo->db->coll;$query = array('s'=>array('$lt'=>100));$fields = array('a'=>true,'b'=>true);$rs = $coll->findone($query,$fields);
查询collection获得多条记录(mongocollection类)
$coll = $mo->db-&集美大学分数线gt;coll;$query = array('s'=>array('$lt'=>100));$fields = array('a'=>true,'b'=>true);$cursor = $coll->find($query,$fields);//排序$cursor->sort(a反贪反腐电视剧rray(‘字段'=>-1));(-1倒序,1正序)//跳过部分记录$cursor->skip(100);跳过100行//只显示部分记录$cursor->limit猫头鹰王国(100);只显示100行返回一个游标记录对象mongocursor。
针对游标对象mongocursor的操作(mongocursor类)
$cursor = $coll->find($query,$fields);while($cursor->hasnext()){$r = $cursor->getnext();var_dump($r);}//或者$cursor = $coll->find($query,$fields);foreache($cursor as $k=>$v){var_dump($v);}//或者$cursor = $coll->find($query,$fields);$array= iterator_to_array($cursor);
本文发布于:2023-04-07 11:12:48,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/1bab48cffb7940719b03b61f555a91c1.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:mongodb和php的用法详解.doc
本文 PDF 下载地址:mongodb和php的用法详解.pdf
留言与评论(共有 0 条评论) |