Heim  >  Artikel  >  php教程  >  PHP连接MongoDB示例

PHP连接MongoDB示例

WBOY
WBOYOriginal
2016-06-21 08:51:581003Durchsuche

 

 

 

 

PHP连接MongoDB示例:

//这里采用默认连接本机的27017端口,当然你也可以连接远程主机如192.168.0.4:27017,如果端口是27017,端口可以省略

$m = new Mongo();

// 选择comedy数据库,如果以前没该数据库会自动创建,也可以用$m->selectDB("comedy");

$db = $m->comedy;

//选择comedy里面的collection集合,相当于RDBMS里面的表,也-可以使用

$collection = $db->collection;

$db->selectCollection("collection");

//添加一个元素

$obj = array( "title" => "Calvin and Hobbes-".date('i:s'), "author" => "Bill Watterson" );

//将$obj 添加到$collection 集合中

$collection->insert($obj);

//添加另一个元素

$obj = array( "title" => "XKCD-".date('i:s'), "online" => true );

$collection->insert($obj);

//查询所有的记录

$cursor = $collection->find();

//遍历所有集合中的文档

foreach ($cursor as $obj)

{

echo $obj["title"] . "\n";

}

//删除所有数据

$collection->remove();

//删除 name 为hm

$collection->remove(array('name'=>'hm'));

//断开MongoDB连接

$m->close();

?>



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn