라이브러리
[PHP] MongoDBDriverManager::executeReadWriteCommand - 읽고 쓰는 데이터베이스 명령 실행
MongoDB Driver Manager
PHP에서 MongoDB를 사용하기 위해 MongoDB Driver Manager를 사용할 수 있습니다. 이 드라이버는 MongoDB 3.6 버전부터 지원되며, MongoDB 4.0 버전부터는 MongoDB Driver Manager를 사용하는 것이 권장됩니다.
executeReadWriteCommand 메서드
`executeReadWriteCommand` 메서드는 MongoDB에서 읽기/쓰기 명령을 실행하는 메서드입니다. 이 메서드는 MongoDB에서 제공하는 읽기/쓰기 명령을 실행할 수 있습니다. 예를 들어, `find`, `update`, `insert`, `delete` 등이 있습니다.
예제
다음 예제는 `executeReadWriteCommand` 메서드를 사용하여 MongoDB에서 데이터를 삽입하는 방법을 보여줍니다.
#hostingforum.kr
php
use MongoDBClient;
// MongoDB 연결
$client = new Client('mongodb://localhost:27017');
// MongoDB 데이터베이스 선택
$db = $client->mydatabase;
// MongoDB 컬렉션 선택
$collection = $db->mycollection;
// 데이터 삽입
$command = [
'insertOne' => [
'document' => [
'name' => 'John Doe',
'age' => 30,
],
],
];
$result = $collection->executeReadWriteCommand($command);
// 결과 출력
print_r($result);
결과
데이터 삽입이 성공적으로 완료되면, `$result` 변수에는 다음과 같은 결과가 출력됩니다.
#hostingforum.kr
php
Array
(
[insertedCount] => 1
[insertedId] => MongoDBBSONObjectID Object
(
[oid] => 61f4f0c1f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4
)
)
읽기/쓰기 명령 실행
`executeReadWriteCommand` 메서드는 읽기/쓰기 명령을 실행할 수 있습니다. 예를 들어, `find` 명령을 실행하여 데이터를 조회하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$command = [
'find' => [
'filter' => [
'name' => 'John Doe',
],
],
];
$result = $collection->executeReadWriteCommand($command);
// 결과 출력
print_r($result);
결과
데이터 조회가 성공적으로 완료되면, `$result` 변수에는 다음과 같은 결과가 출력됩니다.
#hostingforum.kr
php
Array
(
[cursor] => MongoDBDriverCursor Object
(
[collection] => MongoDBCollection Object
(
[database] => MongoDBDatabase Object
(
[client] => MongoDBClient Object
(
[server] => MongoDBServer Object
(
[host] => localhost:27017
)
)
[name] => mydatabase
)
[name] => mycollection
)
[batchSize] => 1
[session] =>
[type] => 1
)
)
결론
`executeReadWriteCommand` 메서드는 MongoDB에서 읽기/쓰기 명령을 실행하는 메서드입니다. 이 메서드를 사용하여 MongoDB에서 데이터를 삽입, 조회, 수정, 삭제할 수 있습니다. 이 예제에서는 데이터 삽입과 조회를 보여주었습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.