MongoDB游标在Perl中进行哈希处理(MongoDB cursor to hash in Perl)

编程入门 行业动态 更新时间:2024-10-25 11:18:39
MongoDB游标在Perl中进行哈希处理(MongoDB cursor to hash in Perl)

所以,我正在从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;

更多推荐

本文发布于:2023-08-01 09:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1356739.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:游标   MongoDB   Perl   hash   cursor

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!