MongoDB\BSON\Document::toCanonicalExtendedJSON()

将MongoDB\BSON\Document对象转换为扩展的JSON格式

首字母:M

函数名称:MongoDB\BSON\Document::toCanonicalExtendedJSON()

适用版本:MongoDB PHP扩展版本1.2.0及以上

用法: 该函数用于将MongoDB\BSON\Document对象转换为扩展的JSON格式。这种格式是MongoDB的扩展JSON规范,它在标准JSON格式的基础上增加了对MongoDB特定数据类型的支持。

示例:

 'John Doe',
 'age' => 30,
 'email' => 'johndoe@example.com',
 '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": "johndoe@example.com",
 "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()函数。

同首字母函数