php 将 MongoCursor 从 ->find() 转换为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7670036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:08:30  来源:igfitidea点击:

Convert a MongoCursor from ->find() to an array

phpmongodb

提问by jini

$jokes = $collection->find();

How do I convert $jokesinto an array?

如何转换$jokes为数组?

回答by Chris Henry

You can use PHP's iterator_to_arrayfunction, as suggested in example 1 of the MongoCursordocs:

您可以iterator_to_array按照MongoCursor文档示例 1 中的建议使用 PHP 的函数:

$jokes = $collection->find();
$jokesArray = iterator_to_array($jokes);

回答by Mihai Cicu

As a side note to Chris's answer:

作为克里斯回答的旁注:

array iterator_to_array ( Traversable $iterator [, bool $use_keys = true ] )

数组 iterator_to_array ( Traversable $iterator [, bool $use_keys = true ] )

Pay attention to the optional second parameter, if it's set to true (default), the final array will be indexed using the "_id" field from each document.

注意可选的第二个参数,如果它设置为 true(默认),最终数组将使用每个文档中的“_id”字段进行索引。

If you applied a sort in the mongo query, the final array might not be what you expected, meaning that the sort order will not be preserved (unless you set the $use_keysparameter to false)

如果您在 mongo 查询中应用了排序,则最终数组可能不是您所期望的,这意味着不会保留排序顺序(除非您将$use_keys参数设置为false

回答by satya prakash patel

iterator_to_arrayis not working for nesting more than 2 levels,

iterator_to_array不适用于嵌套超过 2 个级别,

Using typeMapyou can convert root and its document to array, It will work for any level of nesting

使用typeMap您可以将根及其文档转换为数组,它适用于任何级别的嵌套

findOne($filter,$options)

findOne($filter,$options)

$options = ["typeMap" => ['root' => 'array', 'document' => 'array']];
$collection->findOne(['myId' => $id ], $options); // returns array

find($filter,$options)

查找($过滤器,$选项)

$options = ["typeMap" => ['root' => 'array', 'document' => 'array']];
$collection->find(['myId' => $id ], $options)->toArray(); 

回答by Mahesh Giri

iterator_to_array()forces the driver to load all of the results into memory, so do not do this for result sets that are larger than memory!use this

iterator_to_array()强制驱动程序将所有结果加载到内存中,因此对于大于内存的结果集不要这样做!用这个

$jokes = $collection->find();
foreach ($jokes as $joke) {
    var_dump($joke);
}

回答by Delcon

a lot easier:

容易得多:

findeOne()->getArrayCopy();

as mentioned before: beware from loading large resultsets and convert them to an array

如前所述:小心加载大型结果集并将它们转换为数组

you can also set your preferences with the typeMap option

您还可以使用 typeMap 选项设置您的首选项

'typeMap' =>[
      'document' => 'array',
       'root' => 'array'
                ]

回答by hassan

in case if someone came to here, you can also use toArraymethod.

如果有人来到这里,您也可以使用toArray方法。

(mongodb >=1.0.0)

MongoDB\Driver\Cursor::toArray — Returns an array containing all results for this cursor

(mongodb >=1.0.0)

MongoDB\Driver\Cursor::toArray — 返回一个包含该游标所有结果的数组

$jokes = $collection->find()->toArray();

or :

或者 :

$jokes = $collection->find();
$jokesArray = $jokes->toArray();

回答by Abhinav

find() basically returns MongoDB cursor http://www.php.net/manual/en/mongocollection.find.php

find() 基本上返回 MongoDB 游标 http://www.php.net/manual/en/mongocollection.find.php

this should work for your case

这应该适用于您的情况

$cursor = $collection->find();
foreach($cursor as $jokes) {
 print_r($jokes);
}