3

I have this following database this is an object already in the database

{ "_id" : ObjectId("001"), "password" : "test", "user" : "test"}

Things add to that collection

1.index over user and password

2.Unique index over user

When i try to inssert the same parameters into the items

my $enter = $db->data->insert({'user'=>'test','password'=>'test'});

$enter returns a value of a ObjectID(if user is repeated it should get a error msg rather than a Objectid)

Through mongo shell

E11000 duplicate key error index: dataofitem.user

its showing error but when i try through Perl module (https://metacpan.org/module/MongoDB)

it was returned as an object id, i was wondering why i did't got the error msg instead of Objectid

2
  • 1
    Would you show your code please and the error message you mentioned? Commented Apr 29, 2012 at 13:25
  • @mugenkenichi added some more info check it out Commented Apr 29, 2012 at 16:25

1 Answer 1

5

MongoDB::Collection::insert does not check for errors.

Try

my $enter = $db->data->insert({'user'=>'test','password'=>'test'}, {safe => 1});

to make the module croak on failure. This is documented in MongoDB::Collection

If you want to check for errors you can also do

my $error = $db->last_error();

as documented here.

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

2 Comments

If safe is true, croak comes into play it just kills the process, How pass the error to a string
As I already answered you can check with last_error if you do not use {safe => 1}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.