PHP 7: API 사용

5011 단어 mongodbphp7
컴 파일 설치 PHP 7
이전 소개 참조:
http://blog.csdn.net/liuxinmingcode/article/details/50319145
컴 파일 설치 PHP 7 Mongdb 확장
#        
yum -y install openldap-devel
wget https://pecl.php.net/get/mongodb-1.1.1.tgz
/home/server/php7/bin/phpize   #       PHP    
./configure --with-php-config=/home/server/php7/bin/php-config 
make && make install

#    ,    mongodb.so   lib/php/extensions/no-debug-non-zts-20151012/
  php.ini  
extension=mongodb.so

주:
이전 버 전 은 mongo. so 확장, 오래된 pp - mongodb api 를 사 용 했 습 니 다.
PHP 7 에 서 는 지원 되 지 않 습 니 다. 적어도 현재 지원 되 지 않 습 니 다.
PHP 7 을 지원 하 는 최신 mongodb 컴 파일 후 새 API 만 지원 합 니 다 (mongodb > 2.6. X 버 전)
참고 자료
GITHUB:
https://github.com/mongodb/
홈 페이지:
http://www.mongodb.org/
PHP 공식:
https://pecl.php.net/package/mongodb
http://pecl.php.net/package/mongo  [폐기 되 었 습 니 다. 현재 PHP 5.9999 까지 만 지원 합 니 다.]
API 매 뉴 얼:
http://docs.php.net/manual/en/set.mongodb.php
Mongodb API 조작
Mongodb 연결 초기 화
 
 $manager =  new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
 var_dump($manager);
 
object(MongoDB\Driver\Manager)#1 (3) {
  ["request_id"]=>
  int(1714636915)
  ["uri"]=>
  string(25) "mongodb://localhost:27017"
  ["cluster"]=>
  array(13) {
    ["mode"]=>
    string(6) "direct"
    ["state"]=>
    string(4) "born"
    ["request_id"]=>
    int(0)
    ["sockettimeoutms"]=>
    int(300000)
    ["last_reconnect"]=>
    int(0)
    ["uri"]=>
    string(25) "mongodb://localhost:27017"
    ["requires_auth"]=>
    int(0)
    ["nodes"]=>
    array(...)
    ["max_bson_size"]=>
    int(16777216)
    ["max_msg_size"]=>
    int(50331648)
    ["sec_latency_ms"]=>
    int(15)
    ["peers"]=>
    array(0) {
    }
    ["replSet"]=>
    NULL
  }
}

CURL 조작
$bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]);
$bulk->delete([]);
$bulk->insert(['_id' => 1]);
$bulk->insert(['_id' => 2]);
$bulk->insert(['_id' => 3, 'hello' => 'world']);
$bulk->update(['_id' => 3], ['$set' => ['hello' => 'earth']]);
$bulk->insert(['_id' => 4, 'hello' => 'pluto']);
$bulk->update(['_id' => 4], ['$set' => ['hello' => 'moon']]);
$bulk->insert(['_id' => 3]);
$bulk->insert(['_id' => 4]);
$bulk->insert(['_id' => 5]);


$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);


try {
    $result = $manager->executeBulkWrite('db.collection', $bulk, $writeConcern);
} catch (MongoDB\Driver\Exception\BulkWriteException $e) {
    $result = $e->getWriteResult();


    // Check if the write concern could not be fulfilled
    if ($writeConcernError = $result->getWriteConcernError()) {
        printf("%s (%d): %s
", $writeConcernError->getMessage(), $writeConcernError->getCode(), var_export($writeConcernError->getInfo(), true) ); } // Check if any write operations did not complete at all foreach ($result->getWriteErrors() as $writeError) { printf("Operation#%d: %s (%d)
", $writeError->getIndex(), $writeError->getMessage(), $writeError->getCode() ); } } catch (MongoDB\Driver\Exception\Exception $e) { printf("Other error: %s
", $e->getMessage()); exit; } printf("Inserted %d document(s)
", $result->getInsertedCount()); printf("Updated %d document(s)
", $result->getModifiedCount());

조회 하 다.
$filter = array();
$options = array(
    /* Only return the following fields in the matching documents */
    "projection" => array(
        "title" => 1,
        "article" => 1,
    ),
    "sort" => array(
        "views" => -1,
    ),
    "modifiers" => array(
        '$comment'   => "This is a query comment",
        '$maxTimeMS' => 100,
    ),
);

$query = new MongoDB\Driver\Query($filter, $options);

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$readPreference = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
$cursor = $manager->executeQuery("databaseName.collectionName", $query, $readPreference);

foreach($cursor as $document) {
    var_dump($document);
}

좋은 웹페이지 즐겨찾기