Hi team, I am using .net driver to query mongo in my .net applicaiton. I am using repository pattern. Sometimes for FindAsync(), FindOneAndReplaceAsync(), UpdateOneAsync() methods it is taking longer than 25 minutes times to finish. I have ran a profiler on mongodb for queries longer than 2000 ms but none of these queries I see there.
I observed this in New relic. This is an intermittent issue. Sharing the code below, please help me to figureout.
Ignore the cancellation token, I am not passing it in order to resolve this issue.
// connectionstirng
“ConnectionString”: “abc.in:21011/abc&directConnection=true&serverSelectionTimeoutMS=600000”,
public class MongoLogRepo
{
private readonly IMongoCollection<Nhcx_Mongo_Log> _mongoRepo;
private readonly IMongoCollection _mongoRepoProfile;
public MongoLogRepo(
IOptions<MongoDatabaseSettings> MongoDbSettings)
{
var mongoClient = new MongoClient(
MongoDbSettings.Value.ConnectionString);
var mongoDatabase = mongoClient.GetDatabase(
MongoDbSettings.Value.DatabaseName);
_mongoRepo = mongoDatabase.GetCollection<Nhcx_Mongo_Log>(
MongoDbSettings.Value.NhcxCollectionName);
_mongoRepoProfile = mongoDatabase.GetCollection<ProfileCache>(
MongoDbSettings.Value.ProfileCollectionName);
}
//public async Task<List<Nhcx_Mongo_Log>> GetAsync() =>
// await _mongoRepo.Find(_ => true).ToListAsync();
public async Task<List<Nhcx_Mongo_Log>> GetAsync(string id) =>
await _mongoRepo.Find(x => x._Id == id).ToListAsync();
public async Task<List<Nhcx_Mongo_Log>> GetAsync(Expression<Func<Nhcx_Mongo_Log, bool>> predicate, CancellationToken token)
{
try
{
return await _mongoRepo.Find(predicate).ToListAsync();
}
catch (OperationCanceledException)
{
Console.WriteLine("Operation cancelled");
}
return new List<Nhcx_Mongo_Log>();
}
public async Task<bool> Update(string id, Dictionary<string, object>value)
{
try
{
Console.WriteLine($" {DateTime.Now} in Mongo update");
var updList = new List<UpdateDefinition<Nhcx_Mongo_Log>>();
foreach (var kvp in value)
{
updList.Add(Builders<Nhcx_Mongo_Log>.Update.Set(kvp.Key, kvp.Value));
}
var final = Builders<Nhcx_Mongo_Log>.Update.Combine(updList);
var updateResult = await _mongoRepo.UpdateOneAsync(rec => rec._Id == id, final)
.ConfigureAwait(false);
return updateResult.IsAcknowledged &&
updateResult.ModifiedCount > 0;
}
catch (Exception ex)
{
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(ex));
throw new Exception ("Error while updating the Mongo "+ Newtonsoft.Json.JsonConvert.SerializeObject(ex));
}
}
public async Task<bool> AddUpdate(Nhcx_Mongo_Log record)
{
if (record != null && !string.IsNullOrEmpty(record.CoRelationId))
{
try
{
var updateRecord = await _mongoRepo.FindOneAndReplaceAsync(x => x.CoRelationId == record.CoRelationId, record);
if (updateRecord == null)
await _mongoRepo.InsertOneAsync(record);
return true;
}
catch (Exception ex)
{
throw new Exception("Mongo db transaction failed");
}
}
return false;
}
public Task<List<ProfileCache>>? FetchProfileAsync<T>(Expression<Func<ProfileCache, bool>> predicate)
{
return _mongoRepoProfile.Find(predicate).ToListAsync();
}
public void AddProfiles(List<ProfileCache> profile)
{
_mongoRepoProfile.InsertMany(profile);
}
}
/// Calling methods
var data = await _mongoDb.GetAsync(x => x.CoRelationId == cid, token);
await _mongoDb.AddUpdate(log);