PHP Conference Japan 2024

MongoDB\Driver\ReadPreference::bsonSerialize

(mongodb >=1.2.0)

MongoDB\Driver\ReadPreference::bsonSerialize傳回一個物件以進行 BSON 序列化

說明

final public MongoDB\Driver\ReadPreference::bsonSerialize(): stdClass

參數

此函式沒有參數。

傳回值

傳回一個物件,用於將 ReadPreference 序列化為 BSON。

錯誤/例外

範例

範例 #1 使用主要讀取偏好設定的 MongoDB\Driver\ReadPreference::bsonSerialize()

<?php

$rp
= new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::PRIMARY);
var_dump($rp->bsonSerialize());

echo
"\n", MongoDB\BSON\Document::fromPHP($rp)->toRelaxedExtendedJSON();

?>

以上範例會輸出類似以下的內容:

object(stdClass)#2 (1) {
  ["mode"]=>
  string(7) "primary"
}

{ "mode" : "primary" }

範例 #2 使用 secondary 讀取偏好設定和標籤集的 MongoDB\Driver\ReadPreference::bsonSerialize()

<?php

$rp
= new MongoDB\Driver\ReadPreference(
MongoDB\Driver\ReadPreference::SECONDARY,
[
[
'dc' => 'ny'],
[
'dc' => 'sf', 'use' => 'reporting'],
[]
]
);
var_dump($rp->bsonSerialize());

echo
"\n", MongoDB\BSON\Document::fromPHP($rp)->toRelaxedExtendedJSON();

?>

以上範例會輸出類似以下的內容:

object(stdClass)#2 (2) {
  ["mode"]=>
  string(9) "secondary"
  ["tags"]=>
  array(3) {
    [0]=>
    object(stdClass)#1 (1) {
      ["dc"]=>
      string(2) "ny"
    }
    [1]=>
    object(stdClass)#5 (2) {
      ["dc"]=>
      string(2) "sf"
      ["use"]=>
      string(9) "reporting"
    }
    [2]=>
    object(stdClass)#4 (0) {
    }
  }
}

{ "mode" : "secondary", "tags" : [ { "dc" : "ny" }, { "dc" : "sf", "use" : "reporting" }, {  } ] }

範例 #3 使用 secondary 讀取偏好設定和最大延遲的 MongoDB\Driver\ReadPreference::bsonSerialize()

<?php

$rp
= new MongoDB\Driver\ReadPreference(
MongoDB\Driver\ReadPreference::SECONDARY,
null,
[
'maxStalenessSeconds' => 120]
);
var_dump($rp->bsonSerialize());

echo
"\n", MongoDB\BSON\Document::fromPHP($rp)->toRelaxedExtendedJSON();

?>

以上範例會輸出類似以下的內容:

object(stdClass)#2 (2) {
  ["mode"]=>
  string(9) "secondary"
  ["maxStalenessSeconds"]=>
  int(120)
}

{ "mode" : "secondary", "maxStalenessSeconds" : 120 }

另請參閱

新增筆記

使用者貢獻的筆記

此頁面沒有使用者貢獻的筆記。
To Top