2016-02-11 58 views
1

我在尋找獲取更新通知,比如在我的MongoDB數據庫中插入數據。 我發現MongoRiver.NET庫(Link),可以幫助使用OPLOG,所以我嘗試用這個代碼,但它給我的異常實時監控MongoDB數據庫的更新

class Program 
{ 
    static void Main(string[] args) 
    { 
     try 
     { 
      MainAsync(args).Wait(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e); 
     } 


     Console.WriteLine("Press Enter"); 
     Console.ReadLine(); 
    } 

    static async Task MainAsync(string[] args) 
    { 
     string uri = "mongodb://Host:27017/Bourse"; 
     var client = new MongoClient(uri); 
     var db = client.GetDatabase("Bourse"); 
     var col = db.GetCollection<BOSBourse>("Symbole"); 

     var tailer = new Tailer(client); 
     IOutlet outlet = new BOSBourse(); 
     var stream = new Stream(tailer, outlet); 
     Oplog lastOplog = await tailer.GetMostRecentOplog(); 
     await stream.RunForever(lastOplog); 

    } 

    private async Task RunStream(Stream stream, Oplog startOplog) 
    { 
     var task = stream.RunForever(startOplog); 
     await Task.WhenAny(task, Task.Delay(5000)); 
     stream.Stop(); 
    } 

    class BOSBourse : IOutlet 
    { 
     public ObjectId _id { get; set; } 
     public String Name { get; set; } 
     public Double Price { get; set; } 

     public void UpdateOptime(BsonTimestamp timestamp) 
     { 
      throw new NotImplementedException(); 
     } 

     public void Insert(string databaseName, string collectionName, BsonDocument insertedDocument) 
     { 
      throw new NotImplementedException(); 
     } 

     public void Update(string databaseName, string collectionName, BsonDocument filterDocument, BsonDocument updatedDocument) 
     { 
      throw new NotImplementedException(); 
     } 

     public void Delete(string databaseName, string collectionName, BsonDocument filterDocument) 
     { 
      throw new NotImplementedException(); 
     } 

     public void CreateIndex(string databaseName, string collectionName, BsonDocument indexDocument, BsonDocument optionsDocument) 
     { 
      throw new NotImplementedException(); 
     } 

     public void DeleteIndex(string databaseName, string collectionName, string indexName) 
     { 
      throw new NotImplementedException(); 
     } 

     public void CreateCollection(string databaseName, string collectionName, BsonDocument optionsDocument) 
     { 
      throw new NotImplementedException(); 
     } 

     public void RenameCollection(string databaseName, string collectionName, string newCollectionName) 
     { 
      throw new NotImplementedException(); 
     } 

     public void DeleteCollection(string databaseName, string collectionName) 
     { 
      throw new NotImplementedException(); 
     } 

     public void DeleteDatabase(string databaseName) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 

,這讓我異常:

System.AggregateException :發生一個或多個錯誤。 ---> MongoRiver.MongoRiverException:蒙戈客戶端不配置爲 複製品設定在MongoRiver.Tailer..ctor(IMongoClient客戶端, MongoCollectionSettings oplogCollectionSettings,字符串 oplogCollectionName)

即使經過我添加到代碼,我得到相同的異常

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

https://docs.mongodb.org/manual/reference/connection-string/#standard-connection-string-format –

+0

什麼有用 – Juste3alfaz

+0

在連接字符串中完全錯誤和缺乏複製集選項。 –

回答

-1

這裏是我的解決方案,我解決我的problom做到像這樣

MongoConnectionString = "mongodb://localhost:27017/?replicaSet=rs1"; 

請嘗試

+0

請提供更多信息。爲什麼這是解決方案,解釋會很好。 – onetwo12

+0

查看異常信息,「System.AggregateException:發生了一個或多個錯誤---> MongoRiver.MongoRiverException:Mongo客戶端未配置爲」?replicaSet = rs1的副本集「,只需設置副本集值 – alwin