Call MongoDB Stored Javascript Function in C# .Net

江枫思渺然 提交于 2019-12-01 23:12:21

问题


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

My Stored JavaScript Function GetUserInfo

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

Execution :

db.loadServerScripts();

GetUserInfo();

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"
}

In C#:

IMongoClient _client;
IMongoDatabase _database;

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

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

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

Calling a Stored Procedure in MongoDB via C#

Kindly assist me...


回答1:


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.

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.



来源:https://stackoverflow.com/questions/37719660/call-mongodb-stored-javascript-function-in-c-sharp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!