PHP 关于MongoDB的操作

<?php
header("Content-type:text/html;charset=utf-8");
$m = new MongoClient(); // 连接
$db = $m->test; // 获取名称为 "test" 的数据库
/*集合创建成功
$collection = $db->createCollection("runoob");
echo "集合创建成功";*/


/*插入
$collection = $db->runoob; // 选择集合
$document = array( 
"title" => "MongoDB", 
"description" => "database", 
"likes" => 100,
"url" => "http://www.runoob.com/mongodb/",
"by", "菜鸟教程"
);
$collection->insert($document);
echo "数据插入成功";*/

/*查询
$collection = $db->runoob; // 选择集合

$cursor = $collection->find();
// 迭代显示文档标题
foreach ($cursor as $document) {
echo $document["title"] . "\n";
}*/

/*更新
$collection = $db->runoob; // 选择集合
// 更新文档
$collection->update(array("title"=>"MongoDB"), array('$set'=>array("title"=>"MongoDB 教程")));
// 显示更新后的文档
$cursor = $collection->find();
// 循环显示文档标题
foreach ($cursor as $document) {
echo $document["title"] . "\n";
}*/

/*删除
$collection = $db->runoob; // 选择集合
$collection->remove(array("title"=>"MongoDB 教程"), array("justOne" => true));

// 显示可用文档数据
$cursor = $collection->find();
foreach ($cursor as $document) {
echo $document["title"] . "\n";
}*/

?>

 

posted @ 2016-07-28 16:52  zhuzhipeng  阅读(168)  评论(0编辑  收藏  举报