PHP Conference Japan 2024

MongoDB\Driver\WriteConcern::bsonSerialize

(mongodb >=1.2.0)

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

說明

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

參數

此函式沒有參數。

傳回值

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

錯誤/例外

範例

範例 #1 使用多數寫入關注的 MongoDB\Driver\WriteConcern::bsonSerialize()

<?php

$wc
= new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY);
var_dump($wc->bsonSerialize());

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

?>

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

object(stdClass)#2 (1) {
  ["w"]=>
  string(8) "majority"
}

{ "w" : "majority" }

範例 #2 使用 wtimeout 和 journal 的 MongoDB\Driver\WriteConcern::bsonSerialize()

<?php

$wc
= new MongoDB\Driver\WriteConcern(2, 1000, true);
var_dump($wc->bsonSerialize());

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

?>

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

object(stdClass)#2 (3) {
  ["w"]=>
  int(2)
  ["j"]=>
  bool(true)
  ["wtimeout"]=>
  int(1000)
}

{ "w" : 2, "j" : true, "wtimeout" : 1000 }

另請參閱

新增註記

使用者貢獻的註記

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