MongoDB\BSON\Serializable::bsonSerializeProvides an array or document to serialize as BSON Description
abstract public arraystdClassMongoDB\BSON\DocumentMongoDB\BSON\PackedArray MongoDB\BSON\Serializable::bsonSerialize()
Called during serialization of the object to BSON. The method must return an array, stdClass, MongoDB\BSON\Document, or MongoDB\BSON\PackedArray.
Root documents (e.g. a
MongoDB\BSON\Serializable passed to
MongoDB\BSON\Document::fromPHP) will always be
serialized as a BSON document. For field values, associative arrays and
stdClass instances will be serialized as a BSON
document and sequential arrays (i.e. sequential, numeric indexes starting at
Users are encouraged to include an _id property (e.g. a MongoDB\BSON\ObjectId initialized in the constructor) when returning data for a BSON root document. In the absence of an _id property, the extension or server will generate a MongoDB\BSON\ObjectId for insert or upsert operations, respectively. ParametersThis function has no parameters. Return ValuesAn array, stdClass, MongoDB\BSON\Document, or MongoDB\BSON\PackedArray to be serialized as a BSON array or document. Changelog
ExamplesExample #1 MongoDB\BSON\Serializable::bsonSerialize returning an associative array for root document
The above example will output something similar to: { "_id" : { "$oid" : "56cccdcada14d8755a58c591" }, "foo" : "bar" } Example #2 MongoDB\BSON\Serializable::bsonSerialize returning a sequential array for root document
The above example will output: { "0" : 1, "1" : 2, "2" : 3 } Example #3 MongoDB\BSON\Serializable::bsonSerialize returning an associative array for document field
The above example will output: { "document" : { "foo" : "bar" } } Example #4 MongoDB\BSON\Serializable::bsonSerialize returning a sequential array for document field
The above example will output: { "array" : [ 1, 2, 3 ] } See Also
|