3

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...

8
  • same as stackoverflow.com/questions/23709351/… Commented Jun 9, 2016 at 7:45
  • 1
    It's not working for the current C# driver. There is no extended function Eval. I assist me... Commented Jun 9, 2016 at 7:48
  • api.mongodb.com/csharp/current/html/… isn't that the eval function, that can be used? Commented Jun 9, 2016 at 7:58
  • gist.github.com/jamesikanos/b5897b1693b5c3dd1f87 that can help Commented Jun 9, 2016 at 8:03
  • @ntohl - Dead Link. Kindly check the link. 404 Error - Hmm, we can't reach this page. Commented Jun 9, 2016 at 8:04

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

can you give a sample for use RunCommand to call a stored javascript?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.