Skip to content

Instantly share code, notes, and snippets.

@ashutoshraina
Created January 2, 2014 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashutoshraina/8217451 to your computer and use it in GitHub Desktop.
Save ashutoshraina/8217451 to your computer and use it in GitHub Desktop.
How to tail the mongodb oplog in C# ?
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);
}
}
}
@Neftedollar
Copy link

what is OpLogHandler?

@khavi
Copy link

khavi commented May 18, 2017

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