2

I'd like to insert into my MongoDB using perl the following BSON structure:

{"name" : "BOB", "stuff" : [{"area1": [1,2,3,4,5]}, {"area2": [5,6,7,8,9]}]}

But have had a hard time finding a good example of this. I tried the following:

#!/usr/bin/perl
use MongoDB;
use MongoDB::Database;
use MongoDB::OID;

my $conn = MongoDB::Connection->new;
my $db = $conn->test;
my $users = $db->real_time10;


$users->insert
({
    "name" => "BOB",
    "stuff" => 
              "area1" => [1,2,3,4,5],
              "area2" => [5,6,7,8,9]
});

Which grossly outputs upon query in the mongo shell:

db.real_time10.find()

{ "_id" : ObjectId("4fc912fa000207ec08000000"), "ARRAY(0xa5bdd4)" : "area2", "A
RAY(0x2f2e844)" : null, "name" : "BOB", "stuff" : "area1" }

What is going on? Is there a simple way to do this?

My dream/desired output would be:

> db.real_time10.find()

{ "_id" : ObjectId("4fc912fa000207ec08000000"), "stuff" : {"area1" : [1,2,3,4,5],   
"area2": [5,6,7,8,9]}, "name" : "BOB" }
1
  • See my updated answer, but looks like you figured it out yourself. Good luck. Commented Jun 1, 2012 at 20:03

2 Answers 2

3

Your missing your anonymous-array-constructor (square-brackets) in your example code - but including them in your BSON example. To get your desired output try:

$users->insert({
    "name" => "BOB",
    "stuff" => {
        "area1" => [1,2,3,4,5],
        "area2" => [5,6,7,8,9]
    }
});

By excluding the array constructor it builds up a hash with the supplied array key, value pairs so it would be parsed as the following (which matches your data-dump):

{
    "name" => "BOB",
    "stuff" => "area1",
    [1,2,3,4,5] => "area2",
    [5,6,7,8,9] => undef
}

Note: an array-ref in scalar context will be seen as a string like "ARRAY(0x6052b8)"

Sign up to request clarification or add additional context in comments.

1 Comment

Cool - this is closer to what I want and now I understand my strange output. That being said, when I query my DB i now get this: > db.real_time10.find() { "_id" : ObjectId("4fc91d0600eaf50408000000"), "name" : "BOB", "stuff" : [ "are a1", [ 1, 2, 3, 4, 5 ], "area2", [ 5, 6, 7, 8, 9 ] ] } This seems to be more like an array that contains arrays than an array of objects that correspond to key value pairs. I will update the question with my dream output, haha.
1

Ah, it's this:

#!/usr/bin/perl
use MongoDB;
use MongoDB::Database;
use MongoDB::OID;

my $conn = MongoDB::Connection->new;
my $db = $conn->test;
my $users = $db->real_time10;


$users->insert({
    "name" => "BOB",
    "stuff" => 
          {"area1" => [1,2,3,4,5],
          "area2" => [5,6,7,8,9]}

});

This outputs:

{ "_id" : ObjectId("4fc91f110064e9d40b000000"), "name" : "BOB", "stuff" : { "are
a2" : [ 5, 6, 7, 8, 9 ], "area1" : [ 1, 2, 3, 4, 5 ] } }

Comments

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.