函式名稱:MongoDB\BSON\Document::toCanonicalExtendedJSON()
適用版本:MongoDB PHP擴充套件版本1.2.0及以上
用法: 該函式用於將MongoDB\BSON\Document物件轉換為擴充套件的JSON格式。這種格式是MongoDB的擴充套件JSON規範,它在標準JSON格式的基礎上增加了對MongoDB特定資料型別的支援。
示例:
<?php
// 建立一個MongoDB\BSON\Document物件
$document = new MongoDB\BSON\Document([
'name' => 'John Doe',
'age' => 30,
'email' => '[email protected]',
'address' => new MongoDB\BSON\Document([
'street' => '123 Main St',
'city' => 'New York',
'state' => 'NY'
]),
'interests' => ['reading', 'music', 'sports']
]);
// 將Document物件轉換為擴充套件的JSON格式
$extendedJSON = $document->toCanonicalExtendedJSON();
echo $extendedJSON;
輸出結果:
{
"$document": {
"name": "John Doe",
"age": 30,
"email": "[email protected]",
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY"
},
"interests": ["reading", "music", "sports"]
}
}
注意事項:
- 該函式只能用於MongoDB\BSON\Document物件,不能用於其他型別的文件或資料。
- 輸出的擴充套件JSON格式中,每個文件都會被包裝在"$document"欄位中。
- 如果需要將擴充套件JSON格式轉換回MongoDB\BSON\Document物件,可以使用MongoDB\BSON\fromCanonicalExtendedJSON()函式。