Perl and MongoDB: Returning find results in a String -


relatively simple question, not 1 i've found exact answer - let's have cpan'd mongodb driver, set db data, , want capture results of find text string manipulate in perl script.

use mongodb; use mongodb::database; use mongodb::oid; $conn = mongodb::connection->new; $db = $conn->test; $users = $db->x; $parseable; #ran 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 has     entry! print @objects[0]."\n"; print $objects[0]."\n"; print keys(@objects)."\n"; print keys(@objects[0])."\n"; print "@objects[0]"."\n"; 

these output following on command line, none of them wanted )-=:

len: 1 hash(0x2d48584) hash(0x2d48584) 1 2 hash(0x2d48584) 

what want bson string - want mongoshell returns in string in perl! dream output following:

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

with further added fact can capture string in variable manipulate.

the code below display things fine, won't grab variable manipulation, unfortunately:

#!/usr/bin/perl   use mongodb; use mongodb::database; use mongodb::oid; $conn = mongodb::connection->new; $db = $conn->test; $users = $db->x; $parseable; #ran 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 know how this?

thanks!

try following code :

#!/usr/bin/perl use strict; use warnings;  use data::dumper; use mongodb; use mongodb::collection;  $conn = new mongodb::connection; $db   = $conn->test; $coll = $db->x;  $all = $coll->find(); $dts = $all->next;  use data::dumper; print dumper $dts; 

data::dumper must-have module print complicated perl data structures. understand how data structured way.

then can access directly keys/values :

#!/usr/bin/perl use strict; use warnings;  use mongodb; use mongodb::collection;  $conn = new mongodb::connection; $db   = $conn->test; $coll = $db->x;  $all = $coll->find(); $dts = $all->next;  @a = keys %$dts;  $str = '{ "' .     $a[0] .     '" : objectid("' .     $dts->{_id}. '"), "'.     $a[1].'" : ' .     $dts->{x} .     " }\n";  print $str; 

output is

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

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -