perl - function to print document in json format (mongodb) -
hi i'm using perl query mongodb, when cursor how can print documents in json format
{ "_id" : objectid("51fdbfefb12a69cd35000502"), "biography" : "colombian pop singer, born on february 2, 1977 colombian mother of spanish-italian descent , american father of lebanese-italian descent. shakira known high iq , fluent in spanish, english, italian , portuguese.\r\rwriting songs , belly-dancing since childhood, first album released when mere 14 years old. other spanish-language albums followed, each faring better predecessor, not until 2001 achieved world-wide breakthrough english-language \"laundry service\" album.\r", "imguri" : "http://api.discogs.com/image/a-5530-1218936486.jpeg", "index" : "shakira", "name" : "shakira", "ready" : "yes", "requests" : "0"}
i dont see function in mongodb module migh try this
run_command({printjson => $foo});
but says printjson not function
what can do
thanks
the perl driver return cursor when find:
my $cursor = $collection->find({ => { '$gt' => 42 } });
you can iterator on $cursor
obtain documents, doing this:
while (my $object = $cursor->next) { ... }
in while loop, can access each field of each $object
:
print $object->{i};
if want, can convert them json, need have json cpan module installed. on debian/ubuntu, can use apt-get install libperl-json
that, add use json;
start of script. installed, can put in while
clause:
while (my $object = $cursor->next) { $json = encode_json $object; print "$json\n"; }
Comments
Post a Comment