Skip to content

Instantly share code, notes, and snippets.

@andrevdm
Last active January 2, 2016 01:59
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 andrevdm/8234057 to your computer and use it in GitHub Desktop.
Save andrevdm/8234057 to your computer and use it in GitHub Desktop.
MongoDB C# journal write test
public class Data
{
public ObjectId Id { get; set; }
public string Value { get; set; }
}
static void Main( string[] args )
{
var client = new MongoClient( "mongodb://localhost:27017" );
var server = client.GetServer();
var db = server.GetDatabase( "test" );
var col = db.GetCollection( "test" );
var safe = new WriteConcern {Journal = true};
col.RemoveAll( safe );
col.Insert( new Data{Value = "xxx"}, safe );
col.Insert( new Data{Value = "yyy"}, safe );
Debug.Assert( col.Count() == 2, "!= 2" );
col.RemoveAll( safe );
var x = new Data {Id = ObjectId.GenerateNewId(), Value = "xxx"};
var y = new Data {Id = ObjectId.GenerateNewId(), Value = "yyy"};
col.Update(
Query.EQ( "Value", x.Value ),
Update.Replace( x ),
UpdateFlags.Upsert,
safe );
col.Update(
Query.EQ( "Value", y.Value ),
Update.Replace( y ),
UpdateFlags.Upsert,
safe );
Debug.Assert( col.Count() == 2, "!= 2" );
Debug.Assert( ObjectId.GenerateNewId() != ObjectId.GenerateNewId() );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment