static void Main(string[] args) { var noSql = new MFlixNoSqlDbContext( "mongodb+srv://######:########@freecluster-omk9a.mongodb.net/test?retryWrites=true&w=majority", "sample_mflix" ); FilterDefinition <ChangeStreamDocument <Movie> > filterBuilder = Builders <ChangeStreamDocument <Movie> > .Filter .In(x => x.OperationType, new[] { ChangeStreamOperationType.Insert }.ToList()); var options = new ChangeStreamOptions { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup }; PipelineDefinition <ChangeStreamDocument <Movie>, ChangeStreamDocument <Movie> > pipeline = new EmptyPipelineDefinition <ChangeStreamDocument <Movie> >() .Match <ChangeStreamDocument <Movie>, ChangeStreamDocument <Movie> >(filterBuilder); IChangeStreamCursor <ChangeStreamDocument <Movie> > cursor = noSql.GetCollection <Movie>("movies") .Watch(pipeline, options); using (IEnumerator <ChangeStreamDocument <Movie> > enumerator = cursor.ToEnumerable().GetEnumerator()) { while (enumerator.MoveNext()) { ChangeStreamDocument <Movie> doc = enumerator.Current; Console.WriteLine(doc?.DocumentKey); Console.WriteLine(doc?.FullDocument.Title); } } Console.WriteLine("Done!"); }
protected virtual void Dispose(bool disposing) { if (disposing) { _changeStreamCursor?.Dispose(); _changeStreamCursor = null; GC.Collect(); } }
public BlogUnitOfWork(IMongoDatabase database) { Database = database; BlogSettings = new BlogSettingsRepository(this); Authors = new AuthorRepository(this); Posts = new PostRepository(this); _changeStreamCursor = Database.Watch(); _changeStreamCursor.ForEachAsync((changeStreamDocument) => NotifyEntityChangeWatchers(changeStreamDocument)); }
void startListeningToNotification(CancellationToken cancellationToken) { try { using (IChangeStreamCursor <ChangeStreamDocument <Notification> > cursor = _collection.Watch(_userLogin, cancellationToken)) { try { // might want to actually create an event inside the DAO // that way we are not depending on IChangeStreamCursor in the service // this might make unit tests much harder // Could also return cursor.ToEnumerable(cancellationToken) inside the DAO that way it simply returns IEnumerable<ChangeStreamDocument<Notification>> // ChangeStreamDocument<Notification> can easily be faked by creating a bson document foreach (var notif in cursor.ToEnumerable(cancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); OnNewNotification?.Invoke(new NewNotificationEventArgs( new NewNotificationDTO { Id = notif.FullDocument.Id, Message = notif.FullDocument.Message, DtgUtc = notif.FullDocument.EventDtgUtc, Type = notif.FullDocument.Type } )); } } catch (OperationCanceledException ex) { Console.WriteLine("Operation was canceled"); throw ex; } catch (Exception ex) { notificationServiceErrorCount++; if (notificationServiceErrorCount > 2) { throw ex; } } } } catch (OperationCanceledException) { Console.WriteLine("Notification service cancellation requested"); } catch (Exception ex) { Console.WriteLine("Notification service error:"); Console.WriteLine(ex); } }
public async Task <IChangeStreamCursor <ChangeStreamDocument <Notification> > > GetNotificationChangeStreamCursorAsync() { var mongoClient = new MongoClient(_server); IMongoDatabase database = mongoClient.GetDatabase(_database); var collection = database.GetCollection <Notification>(_collection); var options = new ChangeStreamOptions { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup }; var pipeline = new EmptyPipelineDefinition <ChangeStreamDocument <Notification> >().Match("{ operationType: { $in: [ 'insert' ] } }"); IChangeStreamCursor <ChangeStreamDocument <Notification> > cursor = await collection.WatchAsync(pipeline, options); return(cursor); }
internal void StartChangeStream(IMongoCollection <CompanyDto> targets) { // IChangeStreamCursor<ChangeStreamDocument<CompanyDto>> cursor = targets.Watch(pipeline, options); cursor = targets.Watch(); ChangeStreamDocument <CompanyDto> nextDoc; while (true) { // if there is a new batch of documents in the cursor if (!(cursor.MoveNext() && cursor.Current.Count() == 0)) { nextDoc = cursor.Current.First(); MessageEncountered(nextDoc); } } }
public static int _maxWireVersion <TDocument>(this IChangeStreamCursor <TDocument> cursor) { return((int)Reflector.GetFieldValue(cursor, nameof(_maxWireVersion))); }
public static BsonTimestamp _initialStartAtOperationTime <TDocument>(this IChangeStreamCursor <TDocument> cursor) { return((BsonTimestamp)Reflector.GetFieldValue(cursor, nameof(_initialStartAtOperationTime))); }
public static BsonDocument _initialStartAfter <TDocument>(this IChangeStreamCursor <TDocument> cursor) { return((BsonDocument)Reflector.GetFieldValue(cursor, nameof(_initialStartAfter))); }
public static BsonDocument _postBatchResumeToken <TDocument>(this IChangeStreamCursor <TDocument> cursor) { return((BsonDocument)Reflector.GetFieldValue(cursor, nameof(_postBatchResumeToken))); }
public static ChangeStreamCursor <TDocument> .ResumeValues GetResumeValues <TDocument>(this IChangeStreamCursor <TDocument> cursor) { return((ChangeStreamCursor <TDocument> .ResumeValues)Reflector.Invoke(cursor, nameof(GetResumeValues))); }
public static void _documentResumeToken <TDocument>(this IChangeStreamCursor <TDocument> cursor, BsonDocument value) { Reflector.SetFieldValue(cursor, nameof(_documentResumeToken), value); }
internal ChangeStreamCursor(IChangeStreamCursor <BsonDocument> cursor, Type outputType) { _cursor = cursor; _convert = Actor.ConvertDocument(outputType); }
public static SemanticVersion _serverVersion <TDocument>(this IChangeStreamCursor <TDocument> cursor) { return((SemanticVersion)Reflector.GetFieldValue(cursor, nameof(_serverVersion))); }