1

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!

1
  • I am very interested about who downvoted this and why? What is wrong with this question? Commented Jun 5, 2012 at 12:46

1 Answer 1

2

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 }
Sign up to request clarification or add additional context in comments.

3 Comments

Ah - I see - the output onto the screen here is good, but I have mislead with the question - not only do I want nice terminal output - I want to be able to manipulate the find() result as a string. Substr, indexing, etc. So, in this particular case, while it does output nicely, it still doesn't really capture it in a string for me. Thanks though! I will post results of this to add to the question!
This is a HASH ref, this is NOT a string ;) See perldoc perlref { "_id" : ObjectId("4fcd1f450a121808f4d78bd6"), "x" : 82 } is the output from mongo shell.
Edited AGAIN to better suit your needs =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.