
MongoDBBSONSerializable::bsonSerialize 메서드는 MongoDB에서 사용하는 BSON(Binary Serialized Object Notation) 형식으로 데이터를 serialize하는 역할을 합니다. 이 메서드는 MongoDB에 데이터를 저장하기 전에 데이터를 BSON 형식으로 변환하는 데 사용됩니다.
bsonSerialize 메서드를 사용하는 예시는 다음과 같습니다.
#hostingforum.kr
php
class User implements MongoDBBSONSerializable {
private $id;
private $name;
private $email;
public function bsonSerialize() {
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email
];
}
}
$user = new User();
$user->id = 1;
$user->name = 'John Doe';
$user->email = 'john@example.com';
$collection->insertOne($user);
위의 예시는 User 클래스가 MongoDBBSONSerializable 인터페이스를 구현하고, bsonSerialize 메서드를 오버라이딩하여 데이터를 BSON 형식으로 serialize하는 방법을 보여줍니다.
bsonSerialize 메서드는 MongoDB에 데이터를 저장하기 전에 호출되며, 데이터를 BSON 형식으로 변환하여 MongoDB에 저장합니다.
2025-06-06 11:04