0

I have finally managed to retrive data from mongoDB within PHP. How ever I am not able to retrieve single elements from this array looking. I can only vardump() the cursor. How is it possible to print single elements from this array that seems to be made up of objects?

object(stdClass)#11 (7) { ["_id"]=> object(MongoDB\BSON\ObjectID)#9 (1) { ["oid"]=> string(24) "5a4a2cf55ff0f310cbf1c3a4" } ["Category"]=> string(9) "Allgemein" ["DateAdded"]=> object(MongoDB\BSON\UTCDateTime)#10 (1) { ["milliseconds"]=> string(13) "1514810613331" } ["Name"]=> string(4) "Welt" ["Website"]=> string(11) "www.welt.de" ["Active"]=> bool(true) ["Country"]=> string(2) "DE" } 

I couldnt find anything on goolgle or PHP/mongodb documentation. Why cant I just do $array["_id"]? And how can I retrieve _id for example?

Benjo
  • 41
  • 2
  • 7

1 Answers1

-1

The resource is an object of stdClass. So you need to use:

echo $array->_id;

In case, if you want to use arrays, use get_object_vars() function. That way:

$array = get_object_vars($array);
echo $array["_id"];

And then you can use objects as arrays.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • I think the question means the result comes from using the MongoDB `->find()` method. So the result is not what you seem to think it is. – Neil Lunn May 15 '18 at 08:41
  • @NeilLunn: the linked issues refer to the legacy PHP driver, so I don't think they're pertinent. The example data in the question looks to be a value encountered while iterating on a `MongoDB\Driver\Cursor` object. The [Deserialization from BSON](http://php.net/manual/en/mongodb.persistence.deserialization.php) docs mention that BSON documents are converted to stdClass instances by default (vs. arrays in the legacy driver). This behavior can be changed using [`MongoDB\Driver\Cursior::setTypeMap()`](http://php.net/manual/en/mongodb-driver-cursor.settypemap.php). – jmikola May 21 '18 at 19:12
  • If the [MongoDB PHP library](https://docs.mongodb.com/php-library/) is used on top of the `mongodb` extension, BSON documents will instead by returned as a more convenient class that implements [`ArrayAccess`](http://php.net/arrayaccess). This is discussed in the library's [BSON reference](https://docs.mongodb.com/php-library/current/reference/bson/). – jmikola May 21 '18 at 19:16
  • @jmikola I hate to hijack but the "using the the driver" issue is a common problem with PHP. There are far too many questions where it becomes apparent that the PHPLib has not been used and the lower level interface is all that is installed. The documentation on this can be confusing and it is a "two step" install, which honestly does not help. It usually becomes more apparent when someone wants to run an "aggregate" as a "command". Anything that can make it clearer that PHPLib is what people really want would be welcome. – Neil Lunn May 21 '18 at 19:49
  • @NeilLunn: I acknowledge this is a common problem, but we already advise users to install PHPLIB in the [extension's documentation on PHP.net](http://php.net/manual/en/set.mongodb.php), [GitHub repository readme](https://github.com/mongodb/mongo-php-driver), and our self-hosted [driver portal](https://docs.mongodb.com/ecosystem/drivers/php/). The library is also alluded to in several relevant components of the extension's API docs. – jmikola Jun 11 '18 at 17:36