Created
January 2, 2014 10:35
-
-
Save ashutoshraina/8217451 to your computer and use it in GitHub Desktop.
How to tail the mongodb oplog in C# ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void GetLastEntryInOpLog() | |
{ | |
BsonValue lastId = BsonMinKey.Value; | |
var query = Query.GT("ts", lastId.AsBsonMinKey); | |
var cursor = OpLogHandler.MongoCollection.FindAs<BsonDocument>(query) | |
.SetFlags(QueryFlags.TailableCursor | QueryFlags.AwaitData | QueryFlags.NoCursorTimeout) | |
.SetSortOrder(SortBy.Ascending("$natural")); | |
using (var enumerator = cursor.GetEnumerator()) | |
{ | |
while (enumerator.MoveNext()) | |
{ | |
var document = enumerator.Current; | |
lastId = document["ts"] as BsonTimestamp; | |
Console.WriteLine(string.Format("LastId Is {0}", lastId)); | |
Console.WriteLine(document); | |
} | |
} | |
} |
For the latest version please of MongoDriver 2.0 and above and for Mongo 3.0 version there is a new method.
Please go through this documentation example.
Note there is an error in this method, to fix it please replace the "lastId" with "lastValue". otherwise it perfect.
http://mongodb.github.io/mongo-csharp-driver/2.0/examples/tailable_cursor/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what is
OpLogHandler
?