Mongo返回数组而不是对象

时间:2018-01-25 20:37:29

标签: php mongodb amazon-web-services php-7.1

我有以下问题。我刚刚在我的aws帐户中创建了一个具有相同版本的新mongodb实例,但来自我的php查询的结果以不同的方式出现。

在我以前获取一个对象数组之前,现在我得到了数组数组 例如:

之前:

array(1) {
  [0] =>
  class stdClass#401 (6) {
    public $_id =>
    class MongoDB\BSON\ObjectId#390 (1) {
      public $oid =>
      string(24) "5a685fa82fdc5d031e25451c"
    }
  }
}

立即

array(1) {
  [0]=>
  object(stdClass)#393 (6) {
    ["_id"]=>
    object(MongoDB\BSON\ObjectId)#390 (1) {
      ["oid"]=>
      string(24) "5a685fa82fdc5d031e25451c"
    }
  }
}

之前我曾经使用箭头来访问php中的变量:

$document->_id;

但现在,在以这种方式获得结果后,我需要将所有内容更改为:

$document['_id'];

是否有任何设置(php / mongo服务器)所以我可以像以前一样得到结果?

我使用php mongo驱动程序来查询我的数据库 - 例如:

$query = new Query($filter, $options);
/** @var MongoDB\Driver\Manager $manager */
return $manager->executeQuery($collection,$query)->toArray();

谢谢

2 个答案:

答案 0 :(得分:1)

在游标中,您可以指定它如何使用setTypemap返回结果。我认为['document' => 'stdClass']可以解决问题:

$query = new Query($filter, $options);
/** @var MongoDB\Driver\Manager $manager */
$cursor = $manager->executeQuery($collection, $query);
$cursor->setTypeMap(['document' => 'stdClass']);
return $cursor->toArray();

答案 1 :(得分:0)

$cursor->setTypeMap(['root' => 'array', 'document' => 'array', 'array' => 'array']);

相关问题