Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

MongoDB and WriteConcern

by andal (Hermit)
on Mar 30, 2017 at 09:57 UTC ( [id://1186473]=perlquestion: print w/replies, xml ) Need Help??

andal has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone now how one correctly defines write concern when doing $collection->insert_one()? It seems that I should do something like

$collection->insert_one({....}, { writeConcern => MongoDB::WriteConcern->new( { w => 'majority', j => 1, wtimeout => 10000, } } );
That should be waiting till data is copied to majority of hosts in replica set, so it shall be slower, than default write concern. In practice though I get the same speed of writing as with w => 0 and j => 0. Plus replication looses data if I kill primary host. Do I miss something?

I'm using mongodb 3.2.12 and module MongoDB version 1.6.1

Replies are listed 'Best First'.
Re: MongoDB and WriteConcern
by thanos1983 (Parson) on Mar 30, 2017 at 11:20 UTC

    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!

      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?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1186473]
Approved by marto
Front-paged by Corion
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-05-28 13:46 GMT
Find Nodes?
    Voting Booth?

    No recent polls found