Med Amin

Med Amin

  • NA
  • 16
  • 9.1k

Monitor updates MongoDB databases in realtime

Feb 15 2016 10:03 AM
I look for getting an update notification like inserting data in my MongoDB Database. I find MongoRiver.NET Library (Link) that can help using oplog, So I try with this code, but it give me Exceptions
 
  1. class Program   
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         try  
  6.         {  
  7.             MainAsync(args).Wait();  
  8.         }  
  9.         catch (Exception e)  
  10.         {  
  11.             Console.WriteLine(e);  
  12.         }  
  13.   
  14.   
  15.         Console.WriteLine("Press Enter");  
  16.         Console.ReadLine();  
  17.     }  
  18.   
  19.     static async Task MainAsync(string[] args)  
  20.     {  
  21.         string uri = "mongodb://Host:27017/Bourse";  
  22.         var client = new MongoClient(uri);  
  23.         var db = client.GetDatabase("Bourse");  
  24.         var col = db.GetCollection<BOSBourse>("Symbole");  
  25.   
  26.         var tailer = new Tailer(client);  
  27.         IOutlet outlet = new BOSBourse();  
  28.         var stream = new Stream(tailer, outlet);  
  29.         Oplog lastOplog = await tailer.GetMostRecentOplog();  
  30.         await stream.RunForever(lastOplog);  
  31.   
  32.     }  
  33.   
  34.     private async Task RunStream(Stream stream, Oplog startOplog)  
  35.     {  
  36.         var task = stream.RunForever(startOplog);  
  37.         await Task.WhenAny(task, Task.Delay(5000));  
  38.         stream.Stop();  
  39.     }  
  40.   
  41.     class BOSBourse : IOutlet  
  42.     {  
  43.         public ObjectId _id { getset; }  
  44.         public String Name { getset; }  
  45.         public Double Price { getset; }  
  46.   
  47.         public void UpdateOptime(BsonTimestamp timestamp)  
  48.         {  
  49.             throw new NotImplementedException();  
  50.         }  
  51.   
  52.         public void Insert(string databaseName, string collectionName, BsonDocument insertedDocument)  
  53.         {  
  54.             throw new NotImplementedException();  
  55.         }  
  56.   
  57.         public void Update(string databaseName, string collectionName, BsonDocument filterDocument, BsonDocument updatedDocument)  
  58.         {  
  59.             throw new NotImplementedException();  
  60.         }  
  61.   
  62.         public void Delete(string databaseName, string collectionName, BsonDocument filterDocument)  
  63.         {  
  64.             throw new NotImplementedException();  
  65.         }  
  66.   
  67.         public void CreateIndex(string databaseName, string collectionName, BsonDocument indexDocument, BsonDocument optionsDocument)  
  68.         {  
  69.             throw new NotImplementedException();  
  70.         }  
  71.   
  72.         public void DeleteIndex(string databaseName, string collectionName, string indexName)  
  73.         {  
  74.             throw new NotImplementedException();  
  75.         }  
  76.   
  77.         public void CreateCollection(string databaseName, string collectionName, BsonDocument optionsDocument)  
  78.         {  
  79.             throw new NotImplementedException();  
  80.         }  
  81.   
  82.         public void RenameCollection(string databaseName, string collectionName, string newCollectionName)  
  83.         {  
  84.             throw new NotImplementedException();  
  85.         }  
  86.   
  87.         public void DeleteCollection(string databaseName, string collectionName)  
  88.         {  
  89.             throw new NotImplementedException();  
  90.         }  
  91.   
  92.         public void DeleteDatabase(string databaseName)  
  93.         {  
  94.             throw new NotImplementedException();  
  95.         }  
  96.     }  

And it gives me Exceptions:

System.AggregateException: One or more errors occurred. ---> MongoRiver.MongoRiverException: Mongo client is not configured as a replica set at MongoRiver.Tailer..ctor(IMongoClient client, MongoCollectionSettings oplogCollectionSettings, String oplogCollectionName)

 

Even after I add to the code, I get the same Exception

string uri = "mongodb://Host:27017/?safe=true&connect=replicaset";