略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: MongoDB\BSON\ObjectId::__toString

2024-11-13

MongoDB\BSON\ObjectId::__toString

(mongodb >=1.0.0)

MongoDB\BSON\ObjectId::__toStringReturns the hexidecimal representation of this ObjectId

说明

final public MongoDB\BSON\ObjectId::__toString(): string

参数

此函数没有参数。

返回值

Returns the hexidecimal representation of this ObjectId.

范例

示例 #1 MongoDB\BSON\ObjectId::__toString() example

<?php

var_dump
((string) new MongoDB\BSON\ObjectId());
var_dump((string) new MongoDB\BSON\ObjectId('000000000000000000000001'));

?>

以上例程的输出类似于:

string(24) "56731b49da14d8747d701211"
string(24) "000000000000000000000001"
add a noteadd a note

User Contributed Notes 1 note

up
0
Jared C.
5 years ago
I found I had to manually call the __toString method when I was trying to populate an associative array of mongoguid => stringValue:

        $filter = [];
        $options = [];
        $this->propertyMap = [];
        try {
            $query = new \MongoDB\Driver\Query($filter, $options);
            $cursor = $this->mongo->executeQuery('MyDbName.MyColectionName', $query);
        } catch (Exception $e) {
            echo "Mongo Query failure pulling the collection" . PHP_EOL;
            echo($e->getMessage());
            die("aborting");
        }
        foreach ($cursor as $property) {
            //$this->propertyMap[$property->{_id}->__toString()] = $property->name;
            $this->propertyMap[$property->name] = $property->{_id}->__toString();
        }

You could also do it as $property->{_id} . "" to get the same string result.  Otherwise you end up with an array of MongoDB\BSON\ObjectID objects.

官方地址:https://www.php.net/manual/en/mongodb-bson-objectid.tostring.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3