函数名称:MongoDB\BSON\DBPointer::__toString()
函数描述:返回DBPointer对象的字符串表示形式。
适用版本:MongoDB扩展版本 >= 1.0.0
用法:
public MongoDB\BSON\DBPointer::__toString(): string
示例:
<?php
$collection = (new MongoDB\Client)->test->collection;
$document = $collection->findOne();
// 创建一个DBPointer对象
$dbPointer = new MongoDB\BSON\DBPointer('anotherCollection', $document['_id']);
// 打印DBPointer对象的字符串表示形式
echo $dbPointer->__toString();
?>
输出:
{"$ref":"anotherCollection","$id":ObjectId("5f1c2c3b2a4f4f3d1e6a7b8c")}
说明:
MongoDB\BSON\DBPointer::__toString()
方法将DBPointer对象转换为字符串表示形式。返回的字符串是一个JSON对象,其中$ref
表示关联集合的名称,$id
表示关联文档的ObjectId
。在示例中,$ref
为"anotherCollection"
,$id
为ObjectId("5f1c2c3b2a4f4f3d1e6a7b8c")
。