MongoDB\BSON\Serializable::bsonSerialize

(mongodb >=1.0.0)

MongoDB\BSON\Serializable::bsonSerializeProvides an array or document to serialize as BSON

Beschreibung

abstract public MongoDB\BSON\Serializable::bsonSerialize(): array|object

Called during serialization of the object to BSON. The method must return an array or stdClass.

Root documents (e.g. a MongoDB\BSON\Serializable passed to MongoDB\BSON\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 0) will be serialized as a BSON array.

Users are encouraged to include an _id property (e.g. a MongoDB\BSON\ObjectId initialized in your constructor) when returning data for a BSON root document; otherwise, the driver or database will need to generate a MongoDB\BSON\ObjectId when inserting or upserting the document, respectively.

Parameter-Liste

Diese Funktion besitzt keine Parameter.

Rückgabewerte

An array or stdClass to be serialized as a BSON array or document.

Beispiele

Beispiel #1 MongoDB\BSON\Serializable::bsonSerialize() returning an associative array for root document

<?php

class MyDocument implements MongoDB\BSON\Serializable
{
    private 
$id;

    function 
__construct()
    {
        
$this->id = new MongoDB\BSON\ObjectId;
    }

    function 
bsonSerialize()
    {
        return [
'_id' => $this->id'foo' => 'bar'];
    }
}

$bson MongoDB\BSON\fromPHP(new MyDocument);
echo 
MongoDB\BSON\toJSON($bson), "\n";

?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

{ "_id" : { "$oid" : "56cccdcada14d8755a58c591" }, "foo" : "bar" }

Beispiel #2 MongoDB\BSON\Serializable::bsonSerialize() returning a sequential array for root document

<?php

class MyArray implements MongoDB\BSON\Serializable
{
    function 
bsonSerialize()
    {
        return [
123];
    }
}

$bson MongoDB\BSON\fromPHP(new MyArray);
echo 
MongoDB\BSON\toJSON($bson), "\n";

?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

{ "0" : 1, "1" : 2, "2" : 3 }

Beispiel #3 MongoDB\BSON\Serializable::bsonSerialize() returning an associative array for document field

<?php

class MyDocument implements MongoDB\BSON\Serializable
{
    function 
bsonSerialize()
    {
        return [
'foo' => 'bar'];
    }
}

$value = ['document' => new MyDocument];
$bson MongoDB\BSON\fromPHP($value);
echo 
MongoDB\BSON\toJSON($bson), "\n";

?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

{ "document" : { "foo" : "bar" } }

Beispiel #4 MongoDB\BSON\Serializable::bsonSerialize() returning a sequential array for document field

<?php

class MyArray implements MongoDB\BSON\Serializable
{
    function 
bsonSerialize()
    {
        return [
123];
    }
}

$value = ['array' => new MyArray];
$bson MongoDB\BSON\fromPHP($value);
echo 
MongoDB\BSON\toJSON($bson), "\n";

?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

{ "array" : [ 1, 2, 3 ] }

Siehe auch

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Was genau bedeutet "Vibe Coding"? Ein tiefgehender Blick für Entwickler

In der Welt der Softwareentwicklung gibt es unzählige Wege, wie man an ein Projekt herangeht. Manche schwören auf strikte Planung, andere auf bewährte Algorithmen und wieder andere lassen sich von etwas ganz anderem leiten: ihrem Gefühl. ...

admin

Autor : admin
Kategorie: Software & Web-Development

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

ein word-Dokument automtisch ausfüllen

Da bin ich ja mal gespannt, wann der Spamlink kommt.

Geschrieben von scatello am 28.11.2025 22:02:52
Forum: PHP Developer Forum
ein word-Dokument automtisch ausfüllen

Hallo, ich möchte von meiner Seite aus ein Formular einTextfeld aotomatisch in ein Word-Dokument übertragen Funktioniert so etwas??????? Gruß ...

Geschrieben von stbaumann72 am 28.11.2025 18:42:22
Forum: PHP Developer Forum
2024 Suns Participant Critique: Damion Lee. Gentleman, he can waive a towel!

Enable us toward introduce yet another contemporary contributor in direction of Vivid Aspect, and our minute in opposition to throughout the Pacif ...

Geschrieben von RyanDunn am 28.11.2025 02:20:45
Forum: ASP, PERL, CGI, C und alles andere
Who would the Suns address within an Advancement Draft?

I'm 's July, and enable me inform on your own, it's Really just about anything out below inside the , the thermometer virtually burst at 118 withi ...

Geschrieben von RyanDunn am 28.11.2025 02:19:30
Forum: HTML, JavaScript, AJAX, jQuery, CSS, Bootstrap, LESS