所以,我正在从MongoDB中检索一些值。 我现在的find方法返回一个cursor ,它延迟加载查询结果。 我想将光标存储为哈希,但我可以找到任何方法。 当然,你可以迭代光标并自己填充,但我对自动化方式感兴趣。 例如,在Python中,您可以这样做: myList = list(col.find())将所有游标项目作为列表。
是否有可能在Perl中做类似的事情?
So, I'm retrieving some values from MongoDB. I now that find method returns a cursor, which lazy loads the query results. I want to have the cursor stored as a hash, but I can find any method. Of course, you can iterate over the cursor and fill the has by yourself, but I'm interested in a automated way. For example, in Python, you could do this: myList = list(col.find()) to get all the cursor items as a list.
Would it be possible doing something similar in Perl?
最满意答案
当你调用find ,它返回一个游标对象 ,它将迭代器方法委托给MongoDB :: QueryResult 。 当你做next件事时,它会懒洋洋地一次返回一个文档。 该文件已经是完整的数据集。 这可能是一个哈希引用(或者一个对象,我不知道)。
use Data::Dumper; while (my $object = $cursor->next) { print Dumper $object; }如果你想同时使用所有这些方法,你可以获得一个包含all方法的列表,然后可以将它放入一个数组中并在方便时使用。 这不会按需加载。
my @objects = $cursor->all;When you call find, it returns a cursor object, which delegates iterator methods to a MongoDB::QueryResult. When you do a next on that thing, it returns one document at a time lazily. That document already is the full set of data. This might be a hash reference already (or an object, I don't know).
use Data::Dumper; while (my $object = $cursor->next) { print Dumper $object; }If you want all of them at once, you can get a list with the all method, which you can then put into an array and use at your convenience. This will not load on demand.
my @objects = $cursor->all;更多推荐
发布评论