-
-
Save nordinrahman/802a50cf5d14e1e774fb7d96e1365b51 to your computer and use it in GitHub Desktop.
MongoClient C# Eval Implementation
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 static class MongoClientExtensions | |
{ | |
/// <summary> | |
/// Evaluates the specified javascript within a MongoDb database | |
/// </summary> | |
/// <param name="database">MongoDb Database to execute the javascript</param> | |
/// <param name="javascript">Javascript to execute</param> | |
/// <returns>A BsonValue result</returns> | |
public static async Task<BsonValue> EvalAsync(this IMongoDatabase database, string javascript) | |
{ | |
var client = database.Client as MongoClient; | |
if (client == null) | |
throw new ArgumentException("Client is not a MongoClient"); | |
var function = new BsonJavaScript(javascript); | |
var op = new EvalOperation(database.DatabaseNamespace, function, null); | |
var cluster = client.Cluster; | |
using(var session = cluster.StartSession()) | |
using (var writeBinding = new WritableServerBinding(cluster, session)) | |
{ | |
return await op.ExecuteAsync(writeBinding, CancellationToken.None); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment