I'm using mlab.com mongoDB and i can query into it but i can't insert into it
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);
}
}
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.'
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. [
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 ...
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.
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.
( 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.
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.
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.
I solved it by putting retryWrites=false
at the end of connection string
mongodb://username:[email protected]:62807/robobalancedb?retryWrites=false
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With