centos7下php使用mongodb

  1. 安装mongodb及php-mongodb扩展:
    yum  -y  install  mongodb  mongodb-devel  mongodb-server  php-pecl-mongo
  2. 测试:
    <?php
    $m = new MongoClient();
    $db = $m->mydb;
    $collection = $db->mycol;
    $document = array( 
        "title" => "测试标题", 
        "description" => "mongodb", 
        "likes" => 100,
        "url" => "imlee.top"
    );
    $collection->insert($document);
    $cursor = $collection->find();
    foreach($cursor as $dom){
        var_dump($dom);
    }
  3. 输出:
    array(5) {
    ["_id"]=>
    object(MongoId)#8 (1) {
    ["$id"]=>
    string(24) "5ac0d90bbb8b88b2438b4567"
    }
    ["title"]=>
    string(12) "测试标题"
    ["description"]=>
    string(7) "mongodb"
    ["likes"]=>
    int(100)
    ["url"]=>
    string(9) "imlee.top"
    }

    centos7下php使用mongodb
    centos7下php使用mongodb