Our applications all are running in kubernetes. For backend it is ASP.NET.Core 5 for front end we are using Angular 10 and we configured with ReplicaSets: true for mongodb (also running in kubernetes)
I have implemented full CRUD operation for Mongodb, I would like achieve real time changes when I insert or update something it will automatically appear in Front End without refreshing page.
I tried same approach for Change Operation using this
My code looks like:
// Create action (want to detect this and automatically update get method)
private readonly IMongoDatabase _mongoDatabase;
private readonly MongodbConfig _config;
public MongodbContext(MongodbConfig config)
{
_config = config;
var conventionPack = new ConventionPack {new CamelCaseElementNameConvention()};
ConventionRegistry.Register("camelCase", conventionPack, t => true);
var client = new MongoClient($"mongodb://{_config.Username}:{_config.Password}@{_config.Url}");
_mongoDatabase = client.GetDatabase(_config.Database);
}
public async Task Create(NotificationDto dto, string userId, CancellationToken cancellationToken)
{
var record = _mongoDatabase.GetCollection<NotificationDto>($"notifications-{userId}");
await record.InsertOneAsync(dto, cancellationToken: cancellationToken);
}
// Get collection
public async Task<List<NotificationDto>> Get(string userId, CancellationToken cancellationToken)
{
var record = _mongoDatabase.GetCollection<NotificationDto>($"notifications-{userId}");
FilterDefinition<NotificationDto> notifications = Builders<NotificationDto>.Filter.Eq("userId", userId);
var result = await record.Find(notifications).ToListAsync(cancellationToken);
return result;
}
My watch method for tracking changes
void Watch(string userId). // used BsonDocument instead of dto , does not helped
{
IMongoCollection<NotificationDto> collection =
_mongoDatabase.GetCollection<NotificationDto>($"notifications-{userId}");
ChangeStreamOptions options = new ChangeStreamOptions
{FullDocument = ChangeStreamFullDocumentOption.UpdateLookup};
var pipeline =
new EmptyPipelineDefinition<ChangeStreamDocument<NotificationDto>>().Match(
"{ operationType: { $in: [ 'replace', 'insert', 'update' ] } }");
var changeStream = collection.Watch(pipeline, options).ToEnumerable().GetEnumerator();
changeStream.MoveNext();
ChangeStreamDocument<NotificationDto> next = changeStream.Current;
changeStream.Dispose();
}
This class implemented by interface , I tried to create new method in interface and call watch inside it (by default we can not directly write inside), and call it in program, in startup or directly inside get method, all time it was frozen (unlimited spinning). I have no idea where we need to call Watch and how it will detect changes and also affects to UI to see changes. We are sure our bitnami mongodb configuration is good, all crud working fine, and I am not getting error in code while debugging but it gets stuck in MoveNext() line and starting unlimited loading.
So point is detect changes in mongodb notify api method which is using from UI and see changes.