首页 话题 小组 问答 好文 用户 我的社区 域名交易 唠叨

[函数]MongoDB\BSON\Document::toRelaxedExtendedJSON()函数—用法及示例

发布于 2025-04-29 18:00:49
0
20

函数名: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' => 'johndoe@example.com'
]);

// 将Document对象转换为放松的扩展JSON格式
$json = $document->toRelaxedExtendedJSON();

echo $json;
?>

输出结果:

{
    "_id": {"$oid": "5f9b87a0b54b2d001e4d6d6a"},
    "name": "John Doe",
    "age": 30,
    "email": "johndoe@example.com"
}

在上面的示例中,我们创建了一个MongoDB\BSON\Document对象,并使用toRelaxedExtendedJSON()方法将其转换为放松的扩展JSON格式。转换后的JSON包含了"_id"字段,它是Document对象的默认主键。

评论
一个月内的热帖推荐
啊龙
Lv.1普通用户

9545

帖子

31

小组

3242

积分

赞助商广告
站长交流