监控实时更新MongoDB数据库

时间:2016-02-11 11:50:29

标签: c# mongodb

我希望获得更新通知,例如在MongoDB数据库中插入数据。 我找到可以帮助使用oplog的MongoRiver.NET库(Link),所以我试试这个代码,但它给了我例外

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:发生了一个或多个错误。 ---&GT;   MongoRiver.MongoRiverException:Mongo客户端未配置为   副本设置在MongoRiver.Tailer..ctor(IMongoClient客户端,   MongoCollectionSettings oplogCollectionSettings,String   oplogCollectionName)

即使我添加到代码后,我也会得到相同的异常

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

1 个答案:

答案 0 :(得分:-1)

这是我的解决方案,我解决了我的问题,就像这个

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

拜托,试试吧