라이브러리
[PHP] MongoDBBSONDocument::offsetUnset - ArrayAccess 구현
MongoDBBSONDocument::offsetUnset
`MongoDBBSONDocument::offsetUnset` 메서드는 BSON 문서의 특정 키를 제거합니다. 이 메서드는 BSON 문서의 키를 제거할 때 사용됩니다.
예제
#hostingforum.kr
php
use MongoDBBSONUTCDateTime;
use MongoDBBSONObjectId;
use MongoDBBSONDocument;
// BSON 문서를 생성합니다.
$document = new Document([
'_id' => new ObjectId(),
'name' => 'John Doe',
'age' => 30,
'created_at' => new UTCDateTime(),
]);
// BSON 문서의 키를 제거합니다.
$document->offsetUnset('age');
// BSON 문서를 출력합니다.
print_r($document->toArray());
출력
#hostingforum.kr
php
Array
(
[_id] => MongoDBBSONObjectID Object
(
[$id] => 5f6d5c4c4c4c4c4c4c4c4c4c
)
[name] => John Doe
[created_at] => MongoDBBSONUTCDateTime Object
(
[$timestamp] => 1643723400
)
)
주의사항
`offsetUnset` 메서드는 BSON 문서의 키를 제거할 때 사용됩니다. 이 메서드는 BSON 문서의 키를 제거할 때 사용됩니다. 그러나 이 메서드는 BSON 문서의 `_id` 키를 제거할 수 없습니다. `_id` 키는 BSON 문서의 고유 식별자이기 때문에 제거할 수 없습니다.
예제 (오류)
#hostingforum.kr
php
use MongoDBBSONUTCDateTime;
use MongoDBBSONObjectId;
use MongoDBBSONDocument;
// BSON 문서를 생성합니다.
$document = new Document([
'_id' => new ObjectId(),
'name' => 'John Doe',
'age' => 30,
'created_at' => new UTCDateTime(),
]);
// BSON 문서의 키를 제거합니다. (오류)
$document->offsetUnset('_id');
// BSON 문서를 출력합니다.
print_r($document->toArray());
출력 (오류)
#hostingforum.kr
php
Fatal error: Uncaught Error: Cannot unset _id key in MongoDBBSONDocument
이 예제에서는 `_id` 키를 제거하려고 시도했지만 오류가 발생했습니다. `_id` 키는 BSON 문서의 고유 식별자이기 때문에 제거할 수 없습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.