教程 > mongodb 教程 > 阅读:25

mongodb php——迹忆客-ag捕鱼王app官网

在php中使用mongodb你必须使用 mongodb 的 php驱动。

mongodb php在各平台上的安装及驱动包下载请查看:php安装mongodb扩展驱动


建立连接并选择数据库

要建立连接,我们需要指定数据库名称,如果数据库不存在,则 mongodb 会自动创建它。

以下是连接到数据库的代码片段

mydb;
    
   echo "database mydb selected";
?>

当程序执行时,它会输出以下结果:

connection to database successfully
database mydb selected

创建集合

以下是创建集合的代码片段

mydb;
   echo "database mydb selected";
   $collection = $db->createcollection("mycol");
   echo "collection created succsessfully";
?>

当程序执行时,它会输出以下结果 -

connection to database successfully
database mydb selected
collection created succsessfully

插入文档

要将文档插入 mongodb,请使用insert()方法。

以下是插入文档的代码片段

mydb;
   echo "database mydb selected";
   $collection = $db->mycol;
   echo "collection selected succsessfully";
    
   $document = array( 
      "title" => "mongodb", 
      "description" => "database", 
      "likes" => 100,
      "url" => "http://www.jiyik.com/w/mongodb/",
      "by" => "tutorials point"
   );
    
   $collection->insert($document);
   echo "document inserted successfully";
?>

当程序执行时,它会产生以下结果 -

connection to database successfully
database mydb selected
collection selected succsessfully
document inserted successfully

查找所有文档

要从集合中选择所有文档,请使用 find() 方法。

以下是选择所有文档的代码片段

mydb;
   echo "database mydb selected";
   $collection = $db->mycol;
   echo "collection selected succsessfully";
   $cursor = $collection->find();
   // 依次显示文档的title
    
   foreach ($cursor as $document) {
      echo $document["title"] . "\n";
   }
?>

当程序执行时,它会产生以下结果 -

connection to database successfully
database mydb selected
collection selected succsessfully {
   "title": "mongodb"
}

更新文档

要更新文档,需要使用 update() 方法。

在下面的示例中,我们将插入文档的标题更新为mongodb tutorial。以下是更新文档的代码片段

mydb;
   echo "database mydb selected";
   $collection = $db->mycol;
   echo "collection selected succsessfully";
   // 更新文档
   $collection->update(array("title"=>"mongodb"), 
      array('$set'=>array("title"=>"mongodb tutorial")));
   echo "document updated successfully";
    
   // 现在显示更新后的文档
   $cursor = $collection->find();
    
   // 依次显示文档的title
   echo "updated document";
    
   foreach ($cursor as $document) {
      echo $document["title"] . "\n";
   }
?>

当程序执行时,它会输出以下结果

connection to database successfully
database mydb selected
collection selected succsessfully
document updated successfully
updated document {
   "title": "mongodb tutorial"
}

删除文档

要删除文档,需要使用 remove() 方法。

在以下示例中,我们将删除标题为mongodb tutorial的文档。以下是删除文档的代码片段

mydb;
   echo "database mydb selected";
   $collection = $db->mycol;
   echo "collection selected succsessfully";
   
   // 删除文档
   $collection->remove(array("title"=>"mongodb tutorial"),false);
   echo "documents deleted successfully";
   
   // 显示现在可获取到的文档
   $cursor = $collection->find();
    
   // 依次显示文档的标题
   echo "updated document";
    
   foreach ($cursor as $document) {
      echo $document["title"] . "\n";
   }
?>

当程序执行时,它会输出以下结果 -

connection to database successfully
database mydb selected
collection selected successfully
documents deleted successfully

在上面的例子中,第二个参数是布尔类型,用于remove()方法的justone字段。

其余的 mongodb 方法findone()、save()、limit()、skip()、sort()等的工作原理与上面解释的相同。

查看笔记

扫码一下
查看教程更方便
网站地图