diff --git a/Driver/Core/MongoDatabase.cs b/Driver/Core/MongoDatabase.cs index 71ffc982a8..e286395866 100644 --- a/Driver/Core/MongoDatabase.cs +++ b/Driver/Core/MongoDatabase.cs @@ -397,19 +397,35 @@ namespace MongoDB.Driver { /// /// The code to evaluate. /// Optional arguments (only used when the code is a function with parameters). + /// Whether to run without taking a write lock. /// The result of evaluating the code. public virtual BsonValue Eval( - string code, - params object[] args + BsonJavaScript code, + object[] args, + bool nolock ) { var command = new CommandDocument { { "$eval", code }, - { "args", new BsonArray(args) } + { "args", BsonArray.Create(args), args != null && args.Length > 0 }, + { "nolock", true, nolock } }; var result = RunCommand(command); return result.Response["retval"]; } + /// + /// Evaluates JavaScript code at the server. + /// + /// The code to evaluate. + /// Optional arguments (only used when the code is a function with parameters). + /// The result of evaluating the code. + public virtual BsonValue Eval( + BsonJavaScript code, + params object[] args + ) { + return Eval(code, args, false); // nolock = false + } + /// /// Fetches the document referred to by the DBRef. /// diff --git a/DriverOnlineTests/Core/MongoDatabaseTests.cs b/DriverOnlineTests/Core/MongoDatabaseTests.cs index 63f14a301a..b1da39b293 100644 --- a/DriverOnlineTests/Core/MongoDatabaseTests.cs +++ b/DriverOnlineTests/Core/MongoDatabaseTests.cs @@ -69,6 +69,34 @@ namespace MongoDB.DriverOnlineTests { Assert.IsFalse(database.CollectionExists(collectionName)); } + [Test] + public void TestEvalNoArgs() { + var code = "function() { return 1; }"; + var result = database.Eval(code); + Assert.AreEqual(1, result.ToInt32()); + } + + [Test] + public void TestEvalNoArgsNoLock() { + var code = "function() { return 1; }"; + var result = database.Eval(code, null, true); + Assert.AreEqual(1, result.ToInt32()); + } + + [Test] + public void TestEvalWithArgs() { + var code = "function(x, y) { return x / y; }"; + var result = database.Eval(code, 6, 2); + Assert.AreEqual(3, result.ToInt32()); + } + + [Test] + public void TestEvalWithArgsNoLock() { + var code = "function(x, y) { return x / y; }"; + var result = database.Eval(code, new object[] { 6, 2 }, true); + Assert.AreEqual(3, result.ToInt32()); + } + [Test] public void TestFetchDBRef() { var collectionName = "testdbref";