MongoDB - PHP

MongoDB - PHP 首页 / MongoDB入门教程 / MongoDB - PHP

要将MongoDB与PHP一起使用,您需要使用MongoDB PHP驱动程序,从URL下载PHP驱动程序,确保下载最新版本,现在解压缩并将php_mongo.dll放入您的PHP扩展目录(默认为" ext"),并将以下行添加到php.ini文件中-

extension=php_mongo.dll

连接数据库

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

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

<?php
   //连接到MongoDB.
   $m = new MongoClient();
	
   echo "Connection to database successfully";
   //选择一个数据库
   $db = $m->mydb;
	
   echo "Database mydb selected";
?>

执行程序时,将产生以下输出-

Connection to database successfully
Database mydb selected

创建集合

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

<?php
   //连接到MongoDB.
   $m = new MongoClient();
   echo "Connection to database successfully";
	
   //选择一个数据库
   $db = $m->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()方法。

<?php
   //连接到MongoDB.
   $m = new MongoClient();
   echo "Connection to database successfully";
	
   //选择一个数据库
   $db = $m->mydb;
   echo "Database mydb selected";
   $collection = $db->mycol;
   echo "Collection selected succsessfully";
	
   $document = array( 
      "title" => "MongoDB", 
      "description" => "database", 
      "likes" => 100,
      "url" => "http://www.learnfk.com/mongodb/",
      "by" => "Learnfk point"
   );
	
   $collection->insert($document);
   echo "Document inserted successfully";
?>

执行程序时,将产生以下输出-

Connection to database successfully
Database mydb selected
Collection selected succsessfully
Document inserted successfully

查找数据

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

<?php
   //连接到MongoDB.
   $m = new MongoClient();
   echo "Connection to database successfully";
	
   //选择一个数据库
   $db = $m->mydb;
   echo "Database mydb selected";
   $collection = $db->mycol;
   echo "Collection selected succsessfully";

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

执行程序时,将产生以下输出-

Connection to database successfully
Database mydb selected
Collection selected succsessfully {
   "title": "MongoDB"
}

更新数据

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

在以下示例中,无涯教程将插入文档的标题更新为 MongoDB Tutorial 。

<?php
   //连接到MongoDB.
   $m = new MongoClient();
   echo "Connection to database successfully";
	
   //选择一个数据库
   $db = $m->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();
	
   //迭代游标以显示文档的标题
   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 的文档。

<?php
   //连接到MongoDB.
   $m = new MongoClient();
   echo "Connection to database successfully";
	
   //选择一个数据库
   $db = $m->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 succsessfully
Documents deleted successfully

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

无涯教程网

其余MongoDB方法 findOne(),save(),limit(),skip(),sort()等的工作原理与上述相同。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

邱岳的产品手记 -〔邱岳〕

玩转Git三剑客 -〔苏玲〕

人人都用得上的写作课 -〔涵柏〕

Spark核心原理与实战 -〔王磊〕

成为AI产品经理 -〔刘海丰〕

技术面试官识人手册 -〔熊燚(四火)〕

PyTorch深度学习实战 -〔方远〕

说透元宇宙 -〔方军〕

大型Android系统重构实战 -〔黄俊彬〕

好记忆不如烂笔头。留下您的足迹吧 :)