在C#.Net中调用MongoDB存储的Javascript函数

问题描述:

我需要在C#代码中调用MongoDB存储的JavaScript函数。

I need to call a MongoDB Stored JavaScript Function in C# Code.

我存储的JavaScript函数 GetUserInfo

My Stored JavaScript Function GetUserInfo

function() {
    return db.getCollection('Profession').find({});
}

执行:

db.loadServerScripts();

GetUserInfo();

它返回带有以下文件的集合(这里我只粘贴了2份文件,实际上我有超过10K文件)

It returns the Collection with following documents (Here I pasted only 2 Documents, in real I'm having more than 10K Documents)

{
    "_id" : ObjectId("575845a713d284da0ac2ee81"),
    "Profession_id" : "575841b313d284da0ac2ee7d",
    "Prof_Name" : "Chief Officer"
}

{
    "_id" : ObjectId("575845d213d284da0ac2ee82"),
    "Profession_id" : "575841b313d284da0ac2ee7d",
    "Prof_Name" : "Executive Officer"
}

在C#中:

IMongoClient _client;
IMongoDatabase _database;

_client = new MongoClient();
_database = _client.GetDatabase("SampleDB");

请帮助我如何在C#代码中调用MongoDB存储的JavaScript函数。

Kindly assist me how to call MongoDB Stored JavaScript Function in C# Code.

以下问题使用 Eval 。在最新的驱动程序中,我无法找到扩展函数 _database.Eval

The following Question uses Eval. In the latest driver, I can't able to find the Extended Function _database.Eval

通过C#调用MongoDB中的存储过程

请帮助我...

你可以使用执行eval命令的 RunCommand方法。我们已将其从API本身中删除,因为我们不希望您使用它。请查看这些部分,了解您不应该使用eval的原因,第一个第二个

You can use the RunCommand method to execute the eval command. We've removed it from the API itself because we don't want you using it. Have a look at these sections for why you shouldn't be using eval, first and second.

我强烈建议你重新考虑你的存储过程策略。虽然我赞扬你集中和干掉代码的努力,但MongoDB中的eval会破坏性能和并发性。

I'd highly suggest you rethink your "stored procedure" strategy. While I applaud your efforts to centralize and DRY your code, eval in MongoDB will kill performance and concurrency.