旗下导航:搜·么
当前位置:网站首页 > MySQL教程 > 正文

mongodb和php的用法引见(代码示例)【MySQL教程】,php,mongodb

作者:搜教程发布时间:2019-12-01分类:MySQL教程浏览:55评论:0


导读:本篇文章给人人带来的内容是关于mongodb和php的用法引见(代码示例),有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。Mognodb数据库衔接.默许花样...

本篇文章给人人带来的内容是关于mongodb和php的用法引见(代码示例),有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。

Mognodb数据库衔接.

默许花样

$m = new Mongo();
//这里采纳默许衔接本机的27017端口,固然也能够衔接长途主机如  192.168.0.4:27017,假如端口是27017,端口能够省略。

规范衔接

$m = new Mongo("mongodb://${username}:${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->selectDB("comedy");
$db = $m->comedy;
//挑选comedy内里的collection鸠合,相当于RDBMS内里的表,也能够运用
$collection = $db->collection;
$db->selectCollection("collection");
/*********增添一个元素**************/
$obj = array("title" => "php1", "author" => "Bill Watterson");
//将$obj 增添到$collection 鸠合中
$collection->insert($obj);
/*********增添另一个元素**************/
$obj = array("title" => "huaibei", "online" => true);
$collection->insert($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->close();

带前提的查询

mysql: id = 123
mongo: array(‘id’=>123)
mysql: name link ’%bar%’
mongo: array(‘name’ => new MongoRegex(‘/.*bar.*/i’))
mysql: where id > 10
mongo: array(‘id’ => array(‘$gt’ => 10))
mysql: where id >= 10
mongo: array(‘id’ => array(‘$gte’ => 10))
mysql: where id < 10
mongo: array(‘id’ => array(‘$lt’ => 10))
mysql: where id <= 10
mongo: array(‘id’ => array(‘$lte’ => 10))
mysql: where id > 1 and id < 10
mongo: array(‘id’ => array(‘$gt’ => 1,’$lt’ => 10))
mysql: where id <> 10
mongo: 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 = 9
mongo: array(‘id’ => array(‘$or’ => array(array(‘id’=>2),array(‘id’=>9))))
mysql: order by name asc
mongo: array(‘sort’=>array(‘name’=>1))
mysql: order by name desc
mongo: array(‘sort’=>array(‘name’=>-1))
mysql: limit 0,2
mongo: array(‘limit’=>array(‘offset’=>0,’rows’=>2))
mysql: select name,email
mongo: array(‘name’,'email’)
mysql: select count(name)
mongo: array(‘COUNT’) //注重:COUNT为大写

查询时,每一个Object插进去时都邑自动生成一个奇特的_id,它相当于RDBMS中的主键,用于查询时异常轻易 (_id每一都差别,很像自动增添的id)

<?php
$param = array("name" => "joe");
$collection->insert($param);
$joe = $collection->findOne(array("_id" => $param['_id']));
print_R($joe);
$m->close();

返回效果: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->selectDB(’dbname’);
$coll = $db->collname;
//C:
$db = $mo->dbname;
$coll = $db->collname;
//D:
$db = $mo->dbname;
$coll = $db->selectCollectoin(’collname’);//取得一个collection对象

插进去数据(MongoCollection对象

$coll = $mo->db->foo;
$a = array(’a’=>’b’);
$options = array(’safe’=>true);
$rs  =$coll->insert($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->coll;
$query = array(’s’=>array(’$lt’=>100));
$fields = array(’a’=>true,’b’=>true);
$cursor = $coll->find($query,$fields);
//排序
$cursor->sort(array(‘字段’=>-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);

本篇文章到这里就已悉数完毕了,更多其他精彩内容能够关注ki4网的mongodb视频教程栏目!

以上就是mongodb和php的用法引见(代码示例)的细致内容,更多请关注ki4网别的相干文章!

标签:phpmongodb


欢迎 发表评论: