2013-03-06 80 views
2

我正在嘗試使用MongoDB C#驅動程序編寫一個oplog觀察程序,該驅動程序類似於在Java中使用的一個HereMongoDB C#驅動程序在oplog.rs上的可拖動光標

到目前爲止,我已經成功地寫:

public static void Read() 
{ 
    const string connectionString = "mongodb://127.0.0.1:27017,127.0.0.1:27018/?replicaSet=rs0"; 
    MongoClient mongoClient = new MongoClient(connectionString); 

    MongoDatabase local = mongoClient.GetServer().GetDatabase("local"); 
    MongoCollection opLog = local.GetCollection("oplog.$main"); 
    BsonValue lastId = BsonMinKey.Value; 
    while (true) 
    { 
     var query = Query.GT("_id", lastId); 
     var cursor = opLog.FindAs<BsonDocument>(query) 
        .SetFlags(
         QueryFlags.AwaitData | 
         QueryFlags.TailableCursor | 
         QueryFlags.NoCursorTimeout) 
        .SetSortOrder(SortBy.Ascending("$natural")); 
     using (var enumerator = (MongoCursorEnumerator<BsonDocument>)cursor.GetEnumerator()) 
     { 
      while (true) 
      { 
      // I get a "tailable cursor requested on non capped collection" Exception 
       if (enumerator.MoveNext()) 
       { 
        var document = enumerator.Current; 
        lastId = document["_id"]; 
       } 
       else 
       { 
        if (enumerator.IsDead) 
        { 
         break; 
        } 
        if (!enumerator.IsServerAwaitCapable) 
        { 
         Thread.Sleep(TimeSpan.FromMilliseconds(100)); 
        } 
       } 
      } 
     } 
    } 
} 

我已經在服務器上創建的OPLOG併成功地詢問其使用說明書蒙戈命令行發現here,但我不能找出原因這個例外說明它沒有上限。

+0

我已經看了看[本C#實現(https://groups.google.com/forum/?fromgroups=#!searchin/ mongodb-user/tailable $ 20cursor/mongodb-user/vpuXq-FMfFQ/UT6Z3O2DVj8J),但它創建了一個新的capped集合,並且我對oplog.rs更感興趣。 – mematei 2013-03-06 20:11:50

回答

1

如果您正在使用並設置了複製套件,那麼您有一個oplog。

要查詢oplog,您可以像使用數據庫一樣使用本地數據庫。

然後更改

local.GetCollection("oplog.$main"); 

local.GetCollection("oplog.rs"); 
+0

謝謝你的建議,它解決了這個問題。我很好奇,但是如果你能解釋鏈接例子中「oplog。$ main」的含義是什麼?如果replicaSet未啓用,是否創建文件? – mematei 2013-03-13 18:08:19

+0

不確定名稱「oplog。$ main」。這可能是老式mongod命名的遺留問題。就oplog而言,它是爲複製而創建的,而不是在未創建副本集的情況下創建的。您可能聽說過日記文件,但它們不是oplog - 請參閱http://stackoverflow.com/questions/8970739/how-the-jounal-file-and-oplog-files-are-different-from-each-other -in-mongodb – 2013-03-13 23:13:27

+0

好的,謝謝你的額外信息。事實上,我對oplog而不是日誌文件感興趣。 – mematei 2013-03-14 08:53:28