http://qs321.pair.com?node_id=1186482


in reply to MongoDB and WriteConcern

Hello andal,

I have never used MongoDB and you have not provided us with a minimum example problem replication code to play around so I will just provide you some possible solutions until further update.

Possible solution 1 from (MongoDB::Tutorial):

$users->insert_one( { "name" => "Joe", "age" => 52, "likes" => [qw/skiing math ponies/] });

Possible solution 2 from (MongoDB::Examples):

$db->get_collection( 'users' )->insert_one( { a => 1, b => 1 } );

If none of the examples work in your case, please provide us with minimal working example code that replicates your problem and me and all other monks will be more than happy to help you as much as possible.

Update: regarding the MongoDB::WriteConcern it is defined like this:

$rp = MongoDB::WriteConcern->new(); # w:1, wtimeout: 1000 $rp = MongoDB::WriteConcern->new( w => 'majority', wtimeout => 10000, # milliseconds );

Update2: reading through the MongoDB::Collection and based on your sample of code you are interested in capturing the error on insert_one(). If so it can be done like this:

use Try::Tiny; use Safe::Isa; # provides $_isa try { $coll->insert_one( $doc ) } catch { if ( $_->$_isa("MongoDB::DuplicateKeyError" ) { ... } else { ... } };

Update3: reading through the MongoDB::MongoClient and based on the documentation you could use write_concern, from the documentation:

Returns a MongoDB::WriteConcern object constructed from "w", "write_co +ncern" and "j".

Also from the same module MongoDB::MongoClient you could use $coll->insert() sample of code:

$coll->insert({ name => "John Doe", age => 42 });

Hope this helps.

Seeking for Perl wisdom...on the process of learning...not there...yet!