作者:岁月静好198838 | 来源:互联网 | 2023-09-15 16:41
这期内容当中小编将会给大家带来有关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
数据库操作
插入数据
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&#39; => array(‘$lt&#39; => 10))
mysql: where id <= 10
mongo: array(‘id&#39; => array(‘$lte&#39; => 10))
mysql: where id > 1 and id < 10
mongo: array(‘id&#39; => array(‘$gt&#39; => 1,&#39;$lt&#39; => 10))
mysql: where id <> 10
mongo: array(‘id&#39; => array(‘$ne&#39; => 10))
mysql: where id in(123)
mongo: array(‘id&#39; => array(‘$in&#39; => array(1,2,3)))
mysql: where id not in(123)
mongo: array(‘id&#39; => array(‘$nin&#39; => array(1,2,3)))
mysql: where id = 2 or id = 9
mongo: array(‘id&#39; => array(‘$or&#39; => array(array(‘id&#39;=>2),array(‘id&#39;=>9))))
mysql: order by name asc
mongo: array(‘sort&#39;=>array(‘name&#39;=>1))
mysql: order by name desc
mongo: array(‘sort&#39;=>array(‘name&#39;=>-1))
mysql: limit 0,2
mongo: array(‘limit&#39;=>array(‘offset&#39;=>0,&#39;rows&#39;=>2))
mysql: select name,email
mongo: array(‘name&#39;,&#39;email&#39;)
mysql: select count(name)
mongo: array(‘COUNT&#39;) //注意:COUNT为大写
查询时,每个Object插入时都会自动生成一个独特的_id,它相当于RDBMS中的主键,用于查询时非常方便 (_id每一都不同,很像自动增加的id)
"joe");
$collection->insert($param);
$joe = $collection->findOne(array("_id" => $param[&#39;_id&#39;]));
print_R($joe);
$m->close();
返回结果:Array ( [_id] => MongoId Object ( [$id] => 4fd30e21870da83416000002 ) [name] => joe )
更改字段值
&#39;php1&#39;);
$param = array("title" => &#39;php1&#39;,&#39;author&#39;=>&#39;test&#39;);
$joe = $collection->update($sign, $param);
删除一个数据库
$m -> dropDB(“comedy”);
列出所有可用数据库
$m->listDBs(); //无返回值
创建一个MongoDB对象
删除当前DB
dbname;
$db->drop();
获得当前数据库名
dbname;
$db->_tostring();
选择想要的collection:
//A:
$mo = new Mongo();
$coll = $mo->dbname->collname;//获得一个collection对象
//B:
$db = $mo->selectDB(&#39;dbname&#39;);
$coll = $db->collname;
//C:
$db = $mo->dbname;
$coll = $db->collname;
//D:
$db = $mo->dbname;
$coll = $db->selectCollectoin(&#39;collname&#39;);//获得一个collection对象
插入数据(MongoCollection对象
$coll = $mo->db->foo;
$a = array(&#39;a&#39;=>&#39;b&#39;);
$options = array(&#39;safe&#39;=>true);
$rs =$coll->insert($a,$options);
删除数据库中的记录(MongoCollection对象)
$coll = $mo->db->coll;
$c = array(&#39;a&#39;=>1,&#39;s&#39;=>array(&#39;$lt&#39;=>100));
$options = array(&#39;safe&#39;=>true);
$rs = $coll->remove($c,$options);
更新数据库中的记录(MongoCollection对象)
$coll = $mo->db->coll;
$c = array(&#39;a&#39;=>1,&#39;s&#39;=>array(&#39;$lt&#39;=>100));
$newobj = array(&#39;e&#39;=>&#39;f&#39;,&#39;x&#39;=>&#39;y&#39;);
$options = array(&#39;safe&#39;=>true,&#39;multiple&#39;=>true);
$rs = $coll->remove($c,$newobj,$options);
查询collection获得单条记录(MongoCollection类)
$coll = $mo->db->coll;
$query = array(&#39;s&#39;=>array(&#39;$lt&#39;=>100));
$fields = array(&#39;a&#39;=>true,&#39;b&#39;=>true);
$rs = $coll->findOne($query,$fields);
查询collection获得多条记录(MongoCollection类)
$coll = $mo->db->coll;
$query = array(&#39;s&#39;=>array(&#39;$lt&#39;=>100));
$fields = array(&#39;a&#39;=>true,&#39;b&#39;=>true);
$cursor = $coll->find($query,$fields);
//排序
$cursor->sort(array(‘字段&#39;=>-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);
上述就是小编为大家分享的mongodb怎么在php项目中使用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注编程笔记行业资讯频道。