PHP 7: API 사용
이전 소개 참조:
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);
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
레코드를 업데이트하고 업데이트 전에 동일한 레코드를 삭제하는 방법(nest js & mongoDB)ID로 레코드를 업데이트하고 싶지만 업데이트 전에 동일한 레코드에 이전에 저장된 데이터를 삭제하고 싶습니다. 프로세스는 무엇입니까? 컨트롤러.ts 서비스.ts 나는 이것을 해결하기 위해 이런 식으로 노력하고 있습니다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.