通过C#调用MongoDB中存储过程 [英] Calling a Stored Procedure in MongoDB via C#
本文介绍了通过C#调用MongoDB中存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在system.js保存的功能叫做的addNumbers其中有2个参数:
I have a saved function in system.js called "addNumbers" which has 2 parameters:
function addNumbers( a , b ) {
return a + b;
}
我想通过添加被输入2个数字来执行通过C#这个功能,由使用者。到目前为止,我有以下代码:
I wish to execute this function via C#, by adding 2 numbers which are inputted by a user. So far I have the following code:
MongoClient client = new MongoClient();
MongoServer server = client.GetServer();
MongoDatabase test = server.GetDatabase("test");
Console.WriteLine("Input two numbers: ");
string num1 = Console.ReadLine();
string num2 = Console.ReadLine();
BsonValue bv = test.Eval("addNumbers", num1, num2);
BsonValue bv1 = test.Eval(bv.AsBsonJavaScript.Code, num1, num2);
我做错了吗?由于正在显示任何款项
What I am doing wrong? As no sum is being displayed
推荐答案
所以,我只是做局部测试。随着
So I just did a test locally. With
db.system.js.save({
_id : "myAddFunction" ,
value : function (x, y){ return x + y; }
});
我保存的功能myAddFunction。随着你的代码一点点修改:
I got a saved function myAddFunction. With a little bit modification of your code:
MongoClient client = new MongoClient("mongodb://192.168.122.1/test");
MongoServer server = client.GetServer();
MongoDatabase test = server.GetDatabase("test");
Console.WriteLine("Input two numbers: ");
string num1Str = Console.ReadLine();
string num2Str = Console.ReadLine();
int num1 = int.Parse(num1Str);
int num2 = int.Parse(num2Str);
BsonValue bv = test.Eval("myAddFunction");
BsonValue bv1 = test.Eval(bv.AsBsonJavaScript.Code, num1, num2);
Console.WriteLine(bv1);
现在,我得到显示正确的结果。
Now I get the right result displayed.
这篇关于通过C#调用MongoDB中存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文