Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: MongoDB and WriteConcern

by thanos1983 (Parson)
on Mar 30, 2017 at 11:20 UTC ( [id://1186482]=note: print w/replies, xml ) Need Help??


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!

Replies are listed 'Best First'.
Re^2: MongoDB and WriteConcern
by andal (Hermit) on Mar 31, 2017 at 05:47 UTC

    Sorry, as usually I forgot that people don't know the context of my question. I do know how to use perl and perl modules. I just was hoping that someone knows how to use specific feature of MongoDB via perl module. I guess this is wrong place to ask such questions. It would be better to contact some mailing list of MongoDB module users.

    Anyway, it looks like I've found the answer myself. When connecting to MongoDB one can specify WriteConcern using attributes w, j, and wtimeout. This is described in perldoc MongoDB::MongoClient. So the connecting code would look like

    my $cnx = MongoDB::MongoClient->new( host => 'mongodb://myhost1,myhost2', w => 'majority', j => 1, wtimeout => 10000, replicaSet => 'myset');
    At least this way after killing primary I don't loose data anymore.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1186482]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-18 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found