Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7629411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:49:42+00:00 2026-05-31T05:49:42+00:00

I wrote a script to insert items into mongodb #!/usr/bin/perl use strict; use warnings;

  • 0

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::Connection->new( "host" => "$hostname", 
                                     "port" => $port );
my $db = $conn->test;
my $user_stats = $db->test_stats;

# Insert line
$user_stats->insert({'user_id' => 123, 
                     'pointA'=> 12, 
                     'pointB' => 13, 
                     'total' => 25, } );

my $myStr = $user_stats->find_one();
print Dumper($myStr);

The code work well.
However when I change to insert line to

my $a = "{'user_id' => 123, 
          'pointA' => 12,
          'pointB' => 13,
          'total' => 25}";

$user_stats->insert($a);

It doesn’t work given back error:not a reference at /usr/local/lib/perl5/site_perl/5.12.3/sun4-solaris/MongoDB/Collection.pm line 296.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:49:43+00:00Added an answer on May 31, 2026 at 5:49 am

    The insert method on MongoDB::Collection expects a hash-ref:

    insert ($object, $options?)

    Inserts the given $object into the database and returns it’s id value. $object can be a hash reference, a reference to an array with an even number of elements, or a Tie::IxHash.

    So, the usual approach is to use a hash-ref and your $a is a string, not a hash-ref. The other options are an array-ref that can be easily “cast” to a hash-ref (i.e. it has the form [key, value, key, value, ...]) or a Tie::IxHash (which is a hash that maintains order); your $a string isn’t one of those either.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I finally wrote a script to get Google Analytics data then insert it into
I wrote a perl script a while ago which logged into my online banking
I wrote a script against SQL 2008 which worked fine: INSERT INTO ITEM_TABLE VALUES((SELECT
I need to write a script to insert data selected from one table into
i have wrote a script to produce an array of data but now want
I read from source.sql ( sql script ) file INSERT INTO `Tbl_abc` VALUES (1111,
I wrote a short Ruby script to profile MongoDB, just to see how its
I'm trying to write a SQL script that inserts 2 rows into a table
I wrote a script to export twitter friends as foaf rdf description. Now I'm
I wrote a script in Ruby. I'd like to run it every day at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.