第29章 MongoDB PHP教程
在php中使用mongodb仁兄必须使用 mongodb 的 php驱动。
第29章 MongoDB PHP教程在各平台上的install 及驱动包download 请查看:PHPinstall MongoDB扩展驱动
如果仁兄使用的是 PHP7,请参阅:PHP7 MongoDB install 与使用。
确保连接及选择一个database
为了确保正确连接,仁兄需要指定database name,如果database在mongoDB中不存在,mongoDB会自动创建
代码片段如下:
<?php
$m = new MongoClient(); // 连接默认主机和端口为:mongodb://localhost:27017
$db = $m->test; // 获取名称为 "test" 的database
?>
创建集合
创建集合的代码片段如下:
<?php
$m = new MongoClient(); // 连接
$db = $m->test; // 获取名称为 "test" 的database
$collection = $db->createCollection("liulianjiangcsdn");
echo "集合创建成功";
?>
执行以上程序,输出结果请在输出结果查看
集合创建成功
插入文档
在mongoDB中使用 insert() 方法插入文档:
插入文档代码片段如下:
<?php
$m = new MongoClient(); // 连接到mongodb
$db = $m->test; // 选择一个database
$collection = $db->liulianjiangcsdn; // 选择集合
$document = array(
"title" => "MongoDB",
"description" => "database",
"likes" => 100,
"url" => "http://blog.csdn.net/qq441540598/mongodb/",
"by", "liulianjiangcsdn教程"
);
$collection->insert($document);
echo "数据插入成功";
?>
执行以上程序,输出结果请在输出结果查看
数据插入成功
然后咱们在 mongo 客户端使用 db.liulianjiangcsdn.find().pretty(); 命令查看数据:
查找文档
使用find() 方法来读取集合中的文档。
读取使用文档的代码片段如下:
<?php
$m = new MongoClient(); // 连接到mongodb
$db = $m->test; // 选择一个database
$collection = $db->liulianjiangcsdn; // 选择集合
$cursor = $collection->find();
// 迭代显示文档标题
foreach ($cursor as $document) {
echo $document["title"] . "\n";
}
?>
执行以上程序,输出结果请在输出结果查看
MongoDB
更新文档
使用 update() 方法来更新文档。
以下实例将更新文档中的标题为’ MongoDB 教程’, 代码片段如下:
<pre>
<?php
$m = new MongoClient(); // 连接到mongodb
$db = $m->test; // 选择一个database
$collection = $db->liulianjiangcsdn; // 选择集合
// 更新文档
$collection->update(array("title"=>"MongoDB"), array('$set'=>array("title"=>"MongoDB 教程")));
// 显示更新后的文档
$cursor = $collection->find();
// 循环显示文档标题
foreach ($cursor as $document) {
echo $document["title"] . "\n";
}
?>
执行以上程序,输出结果请在输出结果查看
MongoDB 教程
然后咱们在 mongo 客户端使用 db.liulianjiangcsdn.find().pretty(); 命令查看数据:
删除文档
使用 remove() 方法来删除文档。
以下实例中咱们将移除 ‘title’ 为 ‘MongoDB 教程’ 的一条数据记录。, 代码片段如下:
<?php
$m = new MongoClient(); // 连接到mongodb
$db = $m->test; // 选择一个database
$collection = $db->liulianjiangcsdn; // 选择集合
// 移除文档
$collection->remove(array("title"=>"MongoDB 教程"), array("justOne" => true));
// 显示可用文档数据
$cursor = $collection->find();
foreach ($cursor as $document) {
echo $document["title"] . "\n";
}
?>
除了以上实例外,在php中仁兄还可以使用findOne(), save(), limit(), skip(), sort()等方法来操作Mongodbdatabase。
更多的操作方法可以参考 Mongodb 核心类:http://php.net/manual/zh/mongo.core.php。
本专栏所有文章
第1章 MongoDB 教程 | 第2章 NoSQL 简介教程 |
---|---|
第3章 MongoDB 简介教程 | 第4章 Windows MongoDB教程 |
第5章 Linux MongoDB教程 | 第6章 OSX MongoDB教程 |
第7章 MongoDB 概念解析教程 | 第8章 MongoDB 连接教程 |
第9章 MongoDB 创建数据库教程 | 第10章 MongoDB 删除数据库教程 |
第11章 MongoDB 创建集合教程 | 第12章 MongoDB 删除集合教程 |
第13章 MongoDB 插入文档教程 | 第14章 MongoDB 更新文档教程 |
第15章 MongoDB 删除文档教程 | 第16章 MongoDB 查询文档教程 |
第17章 MongoDB 条件操作符教程 | 第18章 MongoDB $type 操作符教程 |
第19章 MongoDB Limit与Skip方法教程 | 第20章 MongoDB 排序教程 |
第21章 MongoDB 索引教程 | 第22章 MongoDB 聚合教程 |
第23章 MongoDB 复制(副本集)教程 | 第24章 MongoDB 分片教程 |
第25章 MongoDB 备份与恢复教程 | 第26章 MongoDB 监控教程 |
第27章 MongoDB Java教程 | 第28章 第29章 MongoDB PHP教程 扩展教程 |
第29章 第29章 MongoDB PHP教程教程 | 第30章 第29章 MongoDB PHP教程7教程 |
第31章 Node.js MongoDB教程 | 第32章 MongoDB 关系教程 |
第33章 MongoDB 数据库引用教程 | 第34章 MongoDB 覆盖索引查询教程 |
第35章 MongoDB 查询分析教程 | 第36章 MongoDB 原子操作教程 |
第37章 MongoDB 高级索引教程 | 第38章 MongoDB 索引限制教程 |
第39章 MongoDB ObjectId教程 | 第40章 MongoDB Map Reduce教程 |
第41章 MongoDB 全文检索教程 | 第42章 MongoDB 正则表达式教程 |
第43章 MongoDB 管理工具教程 | 第44章 MongoDB GridFS教程 |
第45章 MongoDB 固定集合教程 | 第46章 MongoDB 自动增长教程 |
寄语
本文有榴莲酱CSDN原创,欢迎点赞、转载,博客地址:https://blog.csdn.net/qq441540598
- 物质失落的一切,被精神获取;精神摒弃的一切,反归于物质。
- 一个国家的民族性,初抵它的土地就可以马上区别出来。你有“眼睛”,再平凡的风景在心里一看,全都活了起来。人间到处有青山,何必可以去安排将来的旅程呢?
- 人之所以有一张嘴,而有两只耳朵,起因是听的要比说的多一倍。
- 外祖母坐在窗户下快速地织着花边,线轴快乐地打击着,密密麻麻插满了铜针的枕头在春天的阳光下像金刺猬似的闪光。
- 没有教育,就没有人才;没有人才,就啥也没有。