PinkElephantsOnParade
PinkElephantsOnParade

Reputation: 6592

Perl and MongoDB: Returning find results in a String

Relatively simple question, but not one which I've found an exact answer for - let's say we have CPAN'd the MongoDB driver, set up a DB with some data, and want to capture the results of a find into a text string to manipulate in a perl script.

use MongoDB;
use MongoDB::Database;
use MongoDB::OID;
my $conn = MongoDB::Connection->new;
my $db = $conn->test;
my $users = $db->x;
my $parseable;
#Ran this in mongoshell earlier: db.x.insert({"x":82})
$string = $users->find({"x" => 82}); 
@objects = $string->all;
print "LEN: ".(@objects.length())."\n"; #returns 1....hmmmm...would imply it has my    
entry!
print @objects[0]."\n";
print $objects[0]."\n";
print keys(@objects)."\n";
print keys(@objects[0])."\n";
print "@objects[0]"."\n";

These output the following on the command line, none of them what I wanted )-=:

LEN: 1
HASH(0x2d48584)
HASH(0x2d48584)
1
2
HASH(0x2d48584)

What I do want is the BSON as a string - I want what the mongoshell returns just IN a string in Perl! My dream output would be the following:

{ "_id" : ObjectId("4fcd1f450a121808f4d78bd6"), "x" : 82 }

With the further added fact that I can CAPTURE this string IN A VARIABLE to manipulate.

The code below will display things just fine, but won't grab it into a variable for manipulation, most unfortunately:

#!/usr/bin/perl 

use MongoDB;
use MongoDB::Database;
use MongoDB::OID;
my $conn = MongoDB::Connection->new;
my $db = $conn->test;
my $users = $db->x;
my $parseable;
#Ran this in mongoshell earlier: db.x.insert({"x":82})
$string = $users->find({"x" => 82});

use Data::Dumper; # new import
print Dumper $string->all,0; # call Dumper method

With output:

$VAR1 = {
      '_id' => bless( {
                        'value' => '4fcd1f450a121808f4d78bd6'
                      }, 'MongoDB::OID' ),
      'x' => '82'
    };

Does anybody know how to do this?

Thanks!

Upvotes: 1

Views: 2884

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185434

Try the following code :

#!/usr/bin/perl
use strict;
use warnings;

use Data::Dumper;
use MongoDB;
use MongoDB::Collection;

my $conn = new MongoDB::Connection;
my $db   = $conn->test;
my $coll = $db->x;

my $all = $coll->find();
my $dts = $all->next;

use Data::Dumper;
print Dumper $dts;

Data::Dumper is a must-have module to print complicated Perl data structures. You will understand how the data is structured this way.

Then you can access directly the keys/values :

#!/usr/bin/perl
use strict;
use warnings;

use MongoDB;
use MongoDB::Collection;

my $conn = new MongoDB::Connection;
my $db   = $conn->test;
my $coll = $db->x;

my $all = $coll->find();
my $dts = $all->next;

my @a = keys %$dts;

my $str = '{ "' .
    $a[0] .
    '" : ObjectId("' .
    $dts->{_id}. '"), "'.
    $a[1].'" : ' .
    $dts->{x} .
    " }\n";

print $str;

Output is

{ "_id" : ObjectId("4fcd248f8fa57d73410ec967"), "x" : 82 }

Upvotes: 2

Related Questions