我试图执行可从mongo命令行运行的随机Java脚本,但我试图使用mongo .net core c#驱动程序执行,但在新API中找不到eval函数,因此我创建了扩展方法,例如这个...。但是它没有按预期工作
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);
using (var writeBinding = new WritableServerBinding(client.Cluster, new CoreSessionHandle(NoCoreSession.Instance)))
{
try
{
return await op.ExecuteAsync(writeBinding, CancellationToken.None);
}catch(Exception ex)
{
return await Task.FromResult<string>(ex.InnerException.StackTrace);
}
}
}
test script:
db.collection.updateOne(
{"PageId":NumberInt(12)},
{$set:
{
"PageName":"testpage",
"Section":[{
"SectionId":NumberInt(1),
"Title":"testpage",
"Contents":""}],
"Message":[{
"MessageId":NumberInt(1),
"MessageTypeId":NumberInt(2),
"MessageText":"teswt message."
}]
}
},
{upsert: true}
);
printjson(db.runCommand({getLastError:1}));
我试图执行可从mongo命令行运行的随机Java脚本,但我试图使用mongo .net core c#驱动程序执行,但在新API中找不到eval函数,因此我创建了扩展方法,例如这个...。但是它没有按预期工作