C# 如何从C调用MongoDb中存储的JavaScript#
我正在评估SQL Server数据库到MongoDb的移植 问题是移动存储过程,我阅读了MongoDb存储的JavaScript,我想在.Net中进行一些测试。我已经安装了MongoDb驱动程序2.4.0,并在MongoDb上创建了名为C# 如何从C调用MongoDb中存储的JavaScript#,c#,mongodb,mongodb-.net-driver,C#,Mongodb,Mongodb .net Driver,我正在评估SQL Server数据库到MongoDb的移植 问题是移动存储过程,我阅读了MongoDb存储的JavaScript,我想在.Net中进行一些测试。我已经安装了MongoDb驱动程序2.4.0,并在MongoDb上创建了名为test\u function的函数: function (x) { return x; } 这是我用来尝试调用该函数的代码: MongoClient oMongoClient = new MongoClient(Properties.Settings.D
test\u function
的函数:
function (x)
{
return x;
}
这是我用来尝试调用该函数的代码:
MongoClient oMongoClient = new MongoClient(Properties.Settings.Default.MongoCN);
IMongoDatabase oMongoDatabase = oMongoClient.GetDatabase("test_db");
var result = oMongoDatabase.RunCommand<BsonDocument>("test_function(3)");
MongoClient-oMongoClient=newmongoclient(Properties.Settings.Default.MongoCN);
IMongoDatabase-oMongoDatabase=oMongoClient.GetDatabase(“test_db”);
var result=oMongoDatabase.RunCommand(“test_函数(3)”;
我得到这个错误:
MongoDB.Bson.dll中发生类型为“System.FormatException”的未处理异常
其他信息:JSON阅读器需要一个值,但找到“test_function”
这里也有同样的问题: 我的第一个答案是在那里,但我认为,在这里做更好 我想你可以用这个密码打电话:
var doc = new BsonDocument(new Dictionary<string, string> { { "test_function", "3" }});
var command = new BsonDocumentCommand<int>(doc);
var result = db.RunCommand(command );
我认为您可以使用这种方式运行存储脚本:
var cmd = new JsonCommand<BsonDocument>("{ eval: \"test_function(2)\" }");
var result = db.RunCommand(cmd);
谢谢,它仍然引发异常:MongoDB.Driver.Core.dll中发生了类型为“MongoDB.Driver.MongoCommandException”的未处理异常。其他信息:命令test\u函数失败:没有此类cmd:test\u函数。获得了相同的异常。抱歉,我找到了另一种方法,请参阅答案更新。eval和EvalAsync已在2.4.0驱动程序中删除。我认为从客户端调用存储的js是不可能的。此外,也无法将sql server存储过程迁移到Mongo。我的评估结束后,我意识到这是不可能的。当函数返回一个数字时,它工作正常。现在我想尝试返回文档的列表。我将test_函数改为:函数(x,y){return db.MyDB.find({u id:1});}运行到MongoShell中,返回一个文档。但从dotnet上看,它不起作用。@FDB如果在C#中不能以这种方式获得数据库命令的结果,请问另一个问题,我希望有人能回答;)。
var cmd = new JsonCommand<BsonDocument>("{ eval: \"test_function(2)\" }");
var result = db.RunCommand(cmd);
var intResult = result["retval"].ToInt32();