Gagan
Gagan

Reputation: 35

How can I print only values in perl, result returned from mongodb?

Script

#!/usr/bin/perl -w
use MongoDB;
use Data::Printer;
my $client = MongoDB::MongoClient->new();
my $db   = $client->get_database('students');
my $grades = $db->get_collection('grades');
my $stu = $grades->find->fields({student_id=>1,_id=>0});
while (my $row = $stu->next){
p($row);
}

Output:

\ {
    student_id   198
}
\ {
    student_id   199
}

Intended Output:

198
199

Upvotes: 1

Views: 60

Answers (1)

simbabque
simbabque

Reputation: 54333

Data::Printer is meant for human-readable data inspection. It prints to STDOUT. That's for debugging and looking at data structures during development.

If you want to actually access and print the data to the screen, you need to access it yourself. Data::Printer helps you do this, because it tells you what your data structure looks like.

To print your student_ids (the values of those keys), just print them.

while (my $row = $stu->next){
    print "$row->{student_id}\n";
}

That's it. Consider using say instead as it contains the \n already.

say $row->{student_id};

You need to do use feature 'say'; or use a version pragma that includes it to do that.

Upvotes: 5

Related Questions