函式名:MongoDB\BSON\Document::toRelaxedExtendedJSON()
適用版本:MongoDB PHP擴充套件版本 1.1.0+
用法:
該方法用於將MongoDB\BSON\Document物件轉換為放鬆的擴充套件JSON格式。
public MongoDB\BSON\Document::toRelaxedExtendedJSON ( void ) : string
引數:
- 無引數
返回值:
- 返回一個字串,表示轉換後的放鬆的擴充套件JSON格式。
示例:
<?php
// 匯入MongoDB名稱空間
use MongoDB\BSON\Document;
// 建立一個MongoDB\BSON\Document物件
$document = new Document([
'name' => 'John Doe',
'age' => 30,
'email' => '[email protected]'
]);
// 將Document物件轉換為放鬆的擴充套件JSON格式
$json = $document->toRelaxedExtendedJSON();
echo $json;
?>
輸出結果:
{
"_id": {"$oid": "5f9b87a0b54b2d001e4d6d6a"},
"name": "John Doe",
"age": 30,
"email": "[email protected]"
}
在上面的示例中,我們建立了一個MongoDB\BSON\Document物件,並使用toRelaxedExtendedJSON()方法將其轉換為放鬆的擴充套件JSON格式。轉換後的JSON包含了"_id"欄位,它是Document物件的預設主鍵。