Read MongoDB array into perl and walk data
NickName:Steven Carlson Ask DateTime:2016-04-20T07:57:57

Read MongoDB array into perl and walk data

I am trying to capture an array from my MongoDB database into my Perl script and read each element. This is something that I thought would be simple, but for some dumb reason it is kicking my rearend.

My MongoDB Document (in part)

"members" : [ "5713b2d46d210e51836de591", "me", "you", "him", "her" ],

Perl code

$document = $database -> get_collection('my_collection')->find_one({_id => $oid});
@members = $document->{'members'};  
print Dumper @members;  

foreach $member (@members)  
    {
    print "member = $member\n";
    }
exit;

Output I am getting:

$VAR1 = [
'5713b2d46d210e51836de591',
'me',
'you',
'him',
'her'
];
member = ARRAY(0x47fa398)

Looking at the last line I see that I am being passed a reference to the array instead of the values. So I tried accessing via $member[0] or $member[1] but that just returns the same ARRAY(0x*****).

PLEASE HELP, I am sure it is something stupid.

Thanks!

Steven

Copyright Notice:Content Author:「Steven Carlson」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/36731476/read-mongodb-array-into-perl-and-walk-data

More about “Read MongoDB array into perl and walk data” related questions

Read MongoDB array into perl and walk data

I am trying to capture an array from my MongoDB database into my Perl script and read each element. This is something that I thought would be simple, but for some dumb reason it is kicking my rear...

Show Detail

How to get returned data from mongoDB in Perl?

I read http://api.mongodb.org/perl/current/MongoDB/Examples.html and seems to be only documentation from mongoDB on Perl. How do I get my query result from mongoDB in perl. Let us say into Hash. I ...

Show Detail

insert mongo objects into perl array

Apologies if this is a newbie question. I'm very new to perl. I have a series of strings read from a DB that contain queries, some of which are for mongodb and are in json format (loosely). I am t...

Show Detail

Altering an Array with Array Walk

I have an array that I want to alter. I thought that array_walk would be the way to go but I'm having trouble figuring out how to collect that data from array_walk, deleting the data from the old a...

Show Detail

perl mongodb geospatial array insert fails

I have the following code running in a perl Mojolicious app. The collection it runs against has a 2d index applied to the "loc" property which is 2 element array of the form [ lat, long ]. Mong...

Show Detail

Insert into mongodb with perl

I wrote a script to insert items into mongodb #!/usr/bin/perl use strict; use warnings; use MongoDB; use Data::Dumper; my $hostname = "localhost"; my $port = 27017; my $conn = MongoDB::Connectio...

Show Detail

perl: cannot connect to mongodb server

I am having a problem connecting to the mongodb server from my Perl script. the mongodb server is running ok on localhost port 27017 I can access the mongodb server via the mongod shell and via mo...

Show Detail

perl MongoDB batch_insert - can be done as an upsert?

perl, mongodb 2.4.9, perl MongoDB 0.702.1 When I execute $collection->batch_insert(\@array), it works fine, but is there any way to make to do upsert in this case?

Show Detail

Why does one array_walk work and the other array_walk does not?

I am trying to convert some MS Word characters from a user submitted form. Using array_walk to iterate through the $_POST variables seems to be the best way to do this, but it isn't working when I...

Show Detail

Perl & MongoDB binary data

From the MongoDB manual: By default, all database strings are UTF8. To save images, binaries, and other non-UTF8 data, you can pass the string as a reference to the database. I'm fetching pages and

Show Detail