zoukankan      html  css  js  c++  java
  • php7安装mogodb

    511遇见

    本文教程只在 PHP7 安装使用!!!

    PHP7 Mongdb 扩展安装

    我们使用 pecl 命令来安装:

    $ /usr/local/php7/bin/pecl install mongodb
    

    执行成功后,会输出以下结果:

    ……
    Build process completed successfully
    Installing '/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/mongodb.so'
    install ok: channel://pecl.php.net/mongodb-1.1.7
    configuration option "php_ini" is not set to php.ini location
    You should add "extension=mongodb.so" to php.ini
    

    接下来我们打开 php.ini 文件,添加 extension=mongodb.so 配置。

    可以直接执行以下命令来添加。

    1. $ echo "extension=mongodb.so" >> `/usr/local/php7/bin/php --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||"`

    注意:以上执行的命令中 php7 的安装目录为 /usr/local/php7/,如果你安装在其他目录,需要相应修改 pecl 与 php 命令的路径。

    Mongodb 使用

    PHP7 连接 MongoDB 语法如下:

    $manager = new MongoDBDriverManager("mongodb://localhost:27017");
    

    插入数据

    将 name 为"511遇见" 的数据插入到 test 数据库的 runoob 集合中。

    1. <?php
    2. $bulk = new MongoDBDriverBulkWrite;
    3. $document = ['_id' => new MongoDBBSONObjectID, 'name' => '511遇见'];
    4.  
    5. $_id= $bulk->insert($document);
    6.  
    7. var_dump($_id);
    8.  
    9. $manager = new MongoDBDriverManager("mongodb://localhost:27017");  
    10. $writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 1000);
    11. $result = $manager->executeBulkWrite('test.runoob', $bulk, $writeConcern);
    12. ?>

    读取数据

    这里我们将三个网址数据插入到 test 数据库的 sites 集合,并读取迭代出来:

    1. <?php
    2. $manager = new MongoDBDriverManager("mongodb://localhost:27017");  
    3.  
    4. // 插入数据
    5. $bulk = new MongoDBDriverBulkWrite;
    6. $bulk->insert(['x' => 1, 'name'=>'511遇见', 'url' => 'http://www.511yj.com']);
    7. $bulk->insert(['x' => 2, 'name'=>'baidu', 'url' => 'http://www.baidu.com']);
    8. $bulk->insert(['x' => 3, 'name'=>'bing', 'url' => 'http://www.bing.com']);
    9. $manager->executeBulkWrite('test.sites', $bulk);
    10.  
    11. $filter = ['x' => ['$gt' => 1]];
    12. $options = [
    13.     'projection' => ['_id' => 0],
    14.     'sort' => ['x' => -1],
    15. ];
    16.  
    17. // 查询数据
    18. $query = new MongoDBDriverQuery($filter, $options);
    19. $cursor = $manager->executeQuery('test.sites', $query);
    20.  
    21. foreach ($cursor as $document) {
    22.     print_r($document);
    23. }
    24. ?>

    输出结果为:

    stdClass Object
    (
        [x] => 3
        [name] => bing
        [url] => http://www.bing.com
    )
    stdClass Object
    (
        [x] => 2
        [name] => baidu
        [url] => http://www.baidu.com
    )
    

    更新数据

    接下来我们将更新 test 数据库 sites 集合中 x 为 2 的数据:

    1. <?php
    2. $bulk = new MongoDBDriverBulkWrite;
    3. $bulk->update(
    4.     ['x' => 2],
    5.     ['$set' => ['name' => '图像印记', 'url' => 'images.511yj.com']],
    6.     ['multi' => false, 'upsert' => false]
    7. );
    8.  
    9. $manager = new MongoDBDriverManager("mongodb://localhost:27017");  
    10. $writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 1000);
    11. $result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
    12. ?>

    删除数据

    以下实例删除了 x 为 1 和 x 为 2的数据,注意 limit 参数的区别:

    1. <?php
    2. $bulk = new MongoDBDriverBulkWrite;
    3. $bulk->delete(['x' => 1], ['limit' => 1]);   // limit 为 1 时,删除第一条匹配数据
    4. $bulk->delete(['x' => 2], ['limit' => 0]);   // limit 为 0 时,删除所有匹配数据
    5.  
    6. $manager = new MongoDBDriverManager("mongodb://localhost:27017");  
    7. $writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 1000);
    8. $result = $manager->executeBulkWrite('test.sites', $bulk, $writeConcern);
    9. ?>

    更多使用方法请参考:http://php.net/manual/en/book.mongodb.php

     
     
     

    发布日期: 2016-08-22

    所属分类: Php 标签:  

  • 相关阅读:
    java中Condition类的详细介绍(详解)
    Semaphore原理以及使用总结
    RocketMQ消息存储原理总结(一)
    Es Bucket聚合(桶聚合)总结
    "元素隐式具有 “any” 类型,因为类型“Shared”没有索引签名"问题解决思路
    有关生产环境tomcat java应用报错Caused by: java.lang.OutOfMemoryError: Java heap space的调研
    docker 实践02
    正睿比赛总结day1
    【水】一个神秘玩意:如何分辨出差别很小的颜色?
    AtCoder Regular Contest 125 比赛记录(vp)
  • 原文地址:https://www.cnblogs.com/xiager/p/6855468.html
Copyright © 2011-2022 走看看