标签:
连接
<?php // 连接到mongodb $m = new MongoClient();// 选择一个数据库 $db = $m->mydb;?>
创建集合
<?php // 连接到mongodb $m = new MongoClient();// 选择一个数据库 $db = $m->mydb;
$collection = $db->createCollection("mycol");?>
插入文档
<?php // 连接到mongodb $m = new MongoClient();// 选择一个数据库 $db = $m->mydb;
$collection = $db->mycol;
$document = array( "title" => "MongoDB", "description" => "database", "likes" => 100, "url" => "http://www.w3cschool.cc/mongodb/", "by", "w3cschool.cc" ); $collection->insert($document);?>
查找文档
<?php // 连接到mongodb $m = new MongoClient();// 选择一个数据库 $db = $m->mydb;
$collection = $db->mycol;
$cursor = $collection->find(); // 迭代显示文档标题 foreach ($cursor as $document) { echo $document["title"] . "\n"; } ?>
更新文档
<?php // 连接到mongodb $m = new MongoClient(); $db = $m->mydb;
$collection = $db->mycol;// 更新文档 $collection->update(array("title"=>"MongoDB"), array(‘$set‘=>array("title"=>"MongoDB Tutorial")));// 显示更新后的文档 $cursor = $collection->find(); // 循环显示文档标题foreach ($cursor as $document) { echo $document["title"] . "\n"; } ?>
删除文档
<?php // 连接到mongodb $m = new MongoClient();// 选择一个数据库 $db = $m->mydb;
$collection = $db->mycol;// 移除文档 $collection->remove(array("title"=>"MongoDB Tutorial"),false);// 显示可用文档数据 $cursor = $collection->find(); // iterate cursor to display title of documentsforeach ($cursor as $document) { echo $document["title"] . "\n"; } ?>
标签:
原文地址:http://www.cnblogs.com/LoveJulin/p/4949306.html