zoukankan      html  css  js  c++  java
  • PHP实现的MongoDB数据增删改查

    原文地址:https://www.mongodb.org.cn/drivers/2.html   (该网站为mongoDB官方网站)

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

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

    如果你使用的是 PHP7,请参阅:PHP7 MongoDB 安装与使用

    确保连接及选择一个数据库

    为了确保正确连接,你需要指定数据库名,如果数据库在mongoDB中不存在,mongoDB会自动创建

    代码片段如下:

    1. <?php
    2. $m = new MongoClient(); // 连接默认主机和端口为:mongodb://localhost:27017
    3. $db = $m->test; // 获取名称为 "test" 的数据库
    4. ?>

    创建集合

    创建集合的代码片段如下:

    1. <?php
    2. $m = new MongoClient(); // 连接
    3. $db = $m->test; // 获取名称为 "test" 的数据库
    4. $collection = $db->createCollection("mongo");
    5. echo "集合创建成功";
    6. ?>

    执行以上程序,输出结果如下:

    1. 集合创建成功

    插入文档

    在mongoDB中使用 insert() 方法插入文档:

    插入文档代码片段如下:

    1. <?php
    2. $m = new MongoClient(); // 连接到mongodb
    3. $db = $m->test; // 选择一个数据库
    4. $collection = $db->mongo; // 选择集合
    5. $document = array(
    6. "title" => "MongoDB",
    7. "description" => "database",
    8. "likes" => 100,
    9. "url" => "http://www.mongodb.org.cn/",
    10. "by" => "Mongodb中文网"
    11. );
    12. $collection->insert($document);
    13. echo "数据插入成功";
    14. ?>

    执行以上程序,输出结果如下:

    1. 数据插入成功

    然后我们在 mongo 客户端查看数据

    1. db.mongo.find().pretty();

    输出

    1. {
    2. "_id": ObjectId("57512b3a57c9150f178b4567"),
    3. "title" : "MongoDB",
    4. "description" : "database",
    5. "likes" : NumberLong(100),
    6. "url" : "http://www.mongodb.org.cn/",
    7. "by" : "Mongodb中文网"
    8. }

    查找文档

    使用find() 方法来读取集合中的文档。

    读取使用文档的代码片段如下:

    1. <?php
    2. $m = new MongoClient(); // 连接到mongodb
    3. $db = $m->test; // 选择一个数据库
    4. $collection = $db->runoob; // 选择集合
    5. $cursor = $collection->find(); // 迭代显示文档标题
    6. foreach ($cursor as $document) {
    7. echo $document["title"] . " ";
    8. }
    9. ?>

    执行以上程序,输出结果如下:

    1. MongoDB

    更新文档

    使用 update() 方法来更新文档。

    以下实例将更新文档中的标题为' MongoDB 教程', 代码片段如下:

    1. <?php
    2. $m = new MongoClient(); // 连接到mongodb
    3. $db = $m->test; // 选择一个数据库
    4. $collection = $db->runoob; // 选择集合
    5. // 更新文档
    6. $collection->update(array("title"=>"MongoDB"), array('$set'=>array("title"=>"MongoDB 教程")));
    7. // 显示更新后的文档
    8. $cursor = $collection->find();
    9. // 循环显示文档标题
    10. foreach ($cursor as $document) {
    11. echo $document["title"] . " ";
    12. }
    13. ?>

    执行以上程序,输出结果如下:

    1. MongoDB 教程

    然后我们在 mongo 客户端查看数据是否被更新

    1. db.runoob.find().pretty();

    输出

    1. {
    2. "_id": ObjectId("57512b3a57c9150f178b4567"),
    3. "title" : "MongoDB教程",
    4. "description" : "database",
    5. "likes" : NumberLong(100),
    6. "url" : "http://www.mongodb.org.cn/",
    7. "by" : "Mongodb中文网"
    8. }

    删除文档

    使用 remove() 方法来删除文档。

    以下实例中我们将移除 'title' 为 'MongoDB 教程' 的一条数据记录。, 代码片段如下:

    1. <?php
    2. $m = new MongoClient(); // 连接到mongodb
    3. $db = $m->test; // 选择一个数据库
    4. $collection = $db->mongo; // 选择集合
    5. // 移除文档
    6. $collection->remove(array("title"=>"MongoDB 教程"), array("justOne" => true));
    7. // 显示可用文档数据
    8. $cursor = $collection->find();
    9. foreach ($cursor as $document) {
    10. echo $document["title"] . " ";
    11. }
    12. ?>

    除了以上实例外,在php中你还可以使用findOne(), save(), limit(), skip(), sort()等方法来操作Mongodb数据库。

    更多的操作方法可以参考 Mongodb 核心类:http://php.net/manual/zh/mongo.core.php

  • 相关阅读:
    图表显示磁盘容量
    检测磁盘是否已经准备好
    取消磁盘共享
    远程关闭计算机
    实现注销 关机 重启计算机
    禁止用户关闭计算机
    将计算机设置为休眠状态
    java正则表达式
    JSONArray排序和倒转
    head first 设计模式笔记8-模板方法模式
  • 原文地址:https://www.cnblogs.com/phpk/p/10939310.html
Copyright © 2011-2022 走看看