好吧..我想取“ name”和“ id”值...但是如何? :)
my $results = $collection->find( { })->fields( { name => 1, _id => 1 } );
while (my $doc = $results->next){
foreach my $key (keys %$doc){
my $name = $doc->{$key};
...
print "name: $name\n";
print "id: $id\n";
}
}
答案 0 :(得分:2)
只需访问每个文档中所需的属性即可:) 使用游标迭代器的示例:
my $cursor = $collection->find({})->fields({
name => 1,
_id => 1
});
while (my $doc = $cursor->next){
say "name: $doc->{name}" ;
say " _id: $doc->{_id}" ;
}
或一次性显示所有结果:
map {
say "name: $_->{name}" ;
say " _id: $_->{_id}" ;
} $cursor->all ;