Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix retryWrites in Mongo?

Tags:

c#

mongodb

I'm using mlab.com mongoDB and i can query into it but i can't insert into it

That's my class

public class Main
{
    private MongoUrl _url;
    private MongoClient _client;

    public Main(MongoUrl url)
    {
        url = new MongoUrl("mongodb://username:[email protected]:62807/robobalancedb");
        _url = url;
        _client = new MongoClient(_url);
    }

    public async Task Login([Option]string username, [Option]string password)
    {

        var db = _client.GetDatabase("robobalancedb");

        var col = db.GetCollection<User>("users"); 

        var user = await (await col.FindAsync(x => x.Name == username && x.Password == password)).FirstOrDefaultAsync();

    }

    public async Task Supply([Option(ShortName="u")] string username, [Option(ShortName="a")]double amount, [Option(ShortName="d")]string description)
    {

        var db = _client.GetDatabase("robobalancedb");
        var col = db.GetCollection<OwnerSupply>("ownersupplies");

        var supply = new OwnerSupply(){
            Amount = amount,
            Description = description
        };

        await col.InsertOneAsync(supply);
    }
}

Exception

https://imgur.com/a/cSKNelR

after searching I have tried to add ?retryWrites=true to my connection string mongodb://username:[email protected]:62807/?retryWrites=true/robobalancedb

I got the Exception

MongoDB.Driver.MongoConfigurationException: 'retryWrites has an invalid boolean value of false/robobalancedb.'

and have tried to set RetryWrites in _client field by

_client.Settings.RetryWrites = false; 

got Exception System.InvalidOperationException: 'MongoClientSettings is frozen.'

like image 932
Abdelrhman Elsaid Avatar asked Sep 07 '19 18:09

Abdelrhman Elsaid


People also ask

What is MongoDB retryWrites?

Retryable writes allow MongoDB drivers to automatically retry certain write operations a single time if they encounter network errors, or if they cannot find a healthy primary in the replica set or sharded cluster. [

What is MongoDB SRV?

MongoDB URI syntax. . The use of SRV records eliminates the requirement for every client to pass in a complete set of state information for the cluster. Instead, a single SRV record identifies all the nodes associated with the cluster (and their port numbers) and an associated TXT record defines the options for the URI ...

What is MongoDB transaction?

For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document transactions. With distributed transactions, transactions can be used across multiple operations, collections, databases, documents, and shards.

How do I enable retry writes in MongoDB?

MongoDB Drivers MongoDB 3.6 introduced support for Retryable Writes, but most official MongoDB 3.6 and 4.0-compatible drivers disabled this feature by default. For such drivers, retryable writes could be enabled per connection by including the retryWrites=true option in the connection string for that connection.

Does MongoDB retry upserts for duplicate key errors?

( 1, 2) MongoDB 4.2 will retry certain single-document upserts (update with upsert: true and multi: false) that encounter a duplicate key exception. See Duplicate Key Errors on Upsert for conditions. Prior to MongoDB 4.2, MongoDB would not retry upsert operations that encountered a duplicate key error.

What storage engines support retryable writes in MongoDB?

Retryable writes require a storage engine supporting document-level locking, such as the WiredTiger or in-memory storage engines. The MongoDB version of every node in the cluster must be 3.6 or greater, and the featureCompatibilityVersion of each node in the cluster must be 3.6 or greater.

What is the minimum version of MongoDB for a cluster?

The MongoDB version of every node in the cluster must be 3.6 or greater, and the featureCompatibilityVersion of each node in the cluster must be 3.6 or greater. See setFeatureCompatibilityVersion for more information on the featureCompatibilityVersion flag. Write operations issued with a Write Concern of 0 are not retryable. New in version 4.0.


2 Answers

I solved it by putting retryWrites=false at the end of connection string

mongodb://username:[email protected]:62807/robobalancedb?retryWrites=false

like image 165
Abdelrhman Elsaid Avatar answered Oct 17 '22 11:10

Abdelrhman Elsaid


I fixed it without touching the connexionstring throught code :

var url = new MongoUrl(Configuration.GetValue<string>("MongoConnection"));
var mongoClientSettings = MongoClientSettings.FromUrl(url);
mongoClientSettings.RetryWrites = false;
services.AddSingleton<IMongoClient>(new MongoClient(mongoClientSettings));
like image 34
Xavier W. Avatar answered Oct 17 '22 11:10

Xavier W.