BSON type classes and serialization functions

Table of Contents

The MongoDB\BSON\Binary class

Introduction

Class synopsis

MongoDB\BSON\Binary
class MongoDB\BSON\Binary implements MongoDB\BSON\Type , Serializable , JsonSerializable {
/* Constants */
const integer MongoDB\BSON\Binary::TYPE_GENERIC = 0 ;
const integer MongoDB\BSON\Binary::TYPE_FUNCTION = 1 ;
const integer MongoDB\BSON\Binary::TYPE_OLD_BINARY = 2 ;
const integer MongoDB\BSON\Binary::TYPE_OLD_UUID = 3 ;
const integer MongoDB\BSON\Binary::TYPE_UUID = 4 ;
const integer MongoDB\BSON\Binary::TYPE_MD5 = 5 ;
const integer MongoDB\BSON\Binary::TYPE_USER_DEFINED = 128 ;
/* Methods */
final public __construct ( string $data , integer $type )
final public string getData ( void )
final public integer getType ( void )
final public mixed jsonSerialize ( void )
final public string serialize ( void )
final public string __toString ( void )
final public void unserialize ( string $serialized )
}

Predefined Constants

MongoDB\BSON\Binary::TYPE_GENERIC

Generic binary data.

MongoDB\BSON\Binary::TYPE_FUNCTION

Function.

MongoDB\BSON\Binary::TYPE_OLD_BINARY

Generic binary data (deprecated in favor of MongoDB\BSON\Binary::TYPE_GENERIC).

MongoDB\BSON\Binary::TYPE_OLD_UUID

Universally unique identifier (deprecated in favor of MongoDB\BSON\Binary::TYPE_UUID). When using this type, the Binary's data should be 16 bytes in length.

Historically, other drivers encoded values with this type based on their language conventions (e.g. varying endianness), which makes it non-portable. The PHP driver applies no special handling for encoding or decoding data with this type.

MongoDB\BSON\Binary::TYPE_UUID

Universally unique identifier. When using this type, the Binary's data should be 16 bytes in length and encoded according to » RFC 4122.

MongoDB\BSON\Binary::TYPE_MD5

MD5 hash. When using this type, the Binary's data should be 16 bytes in length.

MongoDB\BSON\Binary::TYPE_USER_DEFINED

User-defined type. While types between 0 and 127 are predefined or reserved, types between 128 and 255 are user-defined and may be used for anything.

Changelog

Version Description
1.2.0 Implements Serializable and JsonSerializable.

The MongoDB\BSON\Decimal128 class

Introduction

BSON type for the » Decimal128 floating-point format, which supports numbers with up to 34 decimal digits (i.e. significant digits) and an exponent range of −6143 to +6144.

Unlike the double BSON type (i.e. float in PHP), which only stores an approximation of the decimal values, the decimal data type stores the exact value. For example, MongoDB\BSON\Decimal128('9.99') has a precise value of 9.99 where as a double 9.99 would have an approximate value of 9.9900000000000002131628….

Note: MongoDB\BSON\Decimal128 is only compatible with MongoDB 3.4+. Attempting to use the BSON type with an earlier version of MongoDB will result in an error.

Class synopsis

MongoDB\BSON\Decimal128
class MongoDB\BSON\Decimal128 implements MongoDB\BSON\Type , Serializable , JsonSerializable {
/* Methods */
final public __construct ([ string $value ] )
final public mixed jsonSerialize ( void )
final public string serialize ( void )
final public string __toString ( void )
final public void unserialize ( string $serialized )
}

Changelog

Version Description
1.2.0 Implements Serializable and JsonSerializable.

The MongoDB\BSON\Javascript class

Introduction

Class synopsis

MongoDB\BSON\Javascript
class MongoDB\BSON\Javascript implements MongoDB\BSON\Type , Serializable , JsonSerializable {
/* Methods */
final public __construct ( string $code [, array|object $scope ] )
final public string getCode ( void )
final public object|null getScope ( void )
final public mixed jsonSerialize ( void )
final public string serialize ( void )
final public string __toString ( void )
final public void unserialize ( string $serialized )
}

Changelog

Version Description
1.2.0 Implements Serializable and JsonSerializable.

The MongoDB\BSON\MaxKey class

Introduction

Class synopsis

MongoDB\BSON\MaxKey
class MongoDB\BSON\MaxKey implements MongoDB\BSON\Type , Serializable , JsonSerializable {
/* Methods */
final public __construct ( void )
final public mixed jsonSerialize ( void )
final public string serialize ( void )
final public void unserialize ( string $serialized )
}

Changelog

Version Description
1.2.0 Implements Serializable and JsonSerializable.

The MongoDB\BSON\MinKey class

Introduction

Class synopsis

MongoDB\BSON\MinKey
class MongoDB\BSON\MinKey implements MongoDB\BSON\Type , Serializable , JsonSerializable {
/* Methods */
final public __construct ( void )
final public mixed jsonSerialize ( void )
final public string serialize ( void )
final public void unserialize ( string $serialized )
}

Changelog

Version Description
1.2.0 Implements Serializable and JsonSerializable.

The MongoDB\BSON\ObjectID class

Introduction

Class synopsis

MongoDB\BSON\ObjectID
class MongoDB\BSON\ObjectID implements MongoDB\BSON\Type , Serializable , JsonSerializable {
/* Methods */
final public __construct ([ string $id ] )
final public integer getTimestamp ( void )
final public mixed jsonSerialize ( void )
final public string serialize ( void )
final public string __toString ( void )
final public void unserialize ( string $serialized )
}

Changelog

Version Description
1.2.0 Implements Serializable and JsonSerializable.

The MongoDB\BSON\Regex class

Introduction

Class synopsis

MongoDB\BSON\Regex
class MongoDB\BSON\Regex implements MongoDB\BSON\Type , Serializable , JsonSerializable {
/* Methods */
final public __construct ( string $pattern [, string $flags = "" ] )
final public string getFlags ( void )
final public string getPattern ( void )
final public mixed jsonSerialize ( void )
final public string serialize ( void )
final public string __toString ( void )
final public void unserialize ( string $serialized )
}

Changelog

Version Description
1.2.0 Implements Serializable and JsonSerializable.

The MongoDB\BSON\Timestamp class

Introduction

Represents a » BSON timestamp, which is an internal MongoDB type not intended for general date storage.

Class synopsis

MongoDB\BSON\Timestamp
class MongoDB\BSON\Timestamp implements MongoDB\BSON\Type , Serializable , JsonSerializable {
/* Methods */
final public __construct ( integer $increment , integer $timestamp )
final public mixed jsonSerialize ( void )
final public string serialize ( void )
final public string __toString ( void )
final public void unserialize ( string $serialized )
}

Changelog

Version Description
1.2.0 Implements Serializable and JsonSerializable.

The MongoDB\BSON\UTCDateTime class

Introduction

Represents a » BSON date.

Class synopsis

MongoDB\BSON\UTCDateTime
class MongoDB\BSON\UTCDateTime implements MongoDB\BSON\Type , Serializable , JsonSerializable {
/* Methods */
final public __construct ([ integer|float|string|DateTimeInterface $milliseconds = NULL ] )
final public mixed jsonSerialize ( void )
final public string serialize ( void )
final public DateTime toDateTime ( void )
final public string __toString ( void )
final public void unserialize ( string $serialized )
}

Changelog

Version Description
1.2.0 Implements Serializable and JsonSerializable.

The MongoDB\BSON\Type interface

Introduction

Class synopsis

MongoDB\BSON\Type
class MongoDB\BSON\Type {
}

The MongoDB\BSON\Persistable interface

Introduction

Classes may implement this interface to take advantage of automatic ODM (object document mapping) behavior in the driver. During serialization, the driver will inject a __pclass property containing the PHP class name into the data returned by MongoDB\BSON\Serializable::bsonSerialize. During unserialization, the same __pclass property will then be used to infer the PHP class (independent of any type map configuration) to be constructed before MongoDB\BSON\Unserializable::bsonUnserialize is invoked. See Persisting Data for additional information.

Note:

Even if MongoDB\BSON\Serializable::bsonSerialize would return a sequential array, injection of the __pclass property will cause the object to be serialized as a BSON document.

Class synopsis

MongoDB\BSON\Persistable
class MongoDB\BSON\Persistable implements MongoDB\BSON\Unserializable , MongoDB\BSON\Serializable {
/* Inherited methods */
abstract public array|object MongoDB\BSON\Serializable::bsonSerialize ( void )
abstract public void MongoDB\BSON\Unserializable::bsonUnserialize ( array $data )
}

The MongoDB\BSON\Serializable interface

Introduction

Classes that implement this interface may return data to be serialized as a BSON array or document in lieu of the object's public properties.

Class synopsis

MongoDB\BSON\Serializable
class MongoDB\BSON\Serializable implements MongoDB\BSON\Type {
/* Methods */
abstract public array|object bsonSerialize ( void )
}

The MongoDB\BSON\Unserializable interface

Introduction

Classes that implement this interface may be specified in a type map for unserializing BSON arrays and documents (both root and embedded).

Class synopsis

MongoDB\BSON\Unserializable
class MongoDB\BSON\Unserializable {
/* Methods */
abstract public void bsonUnserialize ( array $data )
}