3

I'm evaluating the porting of SQL Server database to MongoDb.

The problem is moving stored procedures, I read about MongoDb stored JavaScript and I would like make some test in in .Net. I've installed MongoDb driver 2.4.0 and created this function on MongoDb named test_function:

function (x) 
{
  return x;
}

This is the code I use to try to invoke that function:

MongoClient oMongoClient = new MongoClient(Properties.Settings.Default.MongoCN);
IMongoDatabase oMongoDatabase = oMongoClient.GetDatabase("test_db");
var result = oMongoDatabase.RunCommand<BsonDocument>("test_function(3)");

I get this error:

An unhandled exception of type 'System.FormatException' occurred in MongoDB.Bson.dll Additional information: JSON reader was expecting a value but found 'test_function'.

2 Answers 2

3

The exact same question was here: MongoDB db.runCommand() from C#

My first answer was there, but I think, it's better to do here.

I think you could call with this code:

var doc = new BsonDocument(new Dictionary<string, string> { { "test_function", "3" }});
var command = new BsonDocumentCommand<int>(doc);
var result = db.RunCommand(command );

But, as you could see here, it is really not recommended to use stored procedures this way.

I have found another solution here:

https://gist.github.com/jamesikanos/b5897b1693b5c3dd1f87

With this snippet, you could call your function this way:

db.EvalAsync("test_function(2)").Result
Sign up to request clarification or add additional context in comments.

3 Comments

thanks, it still throws exception: An unhandled exception of type 'MongoDB.Driver.MongoCommandException' occurred in MongoDB.Driver.Core.dll Additional information: Command test_function failed: no such cmd: test_function.
got the same exception. Sorry, i have found another way of doing it, see update of the answer.
eval and EvalAsync has been removed in 2.4.0 driver. I Think it is impossible to call a stored js from the client. Also there's no way to migrate sql server storted procedures to Mongo. My evaluation is ended, i realized it is impossible.
0

I think you can use this way to run a stored script:

var cmd = new JsonCommand<BsonDocument>("{ eval: \"test_function(2)\" }");
var result = db.RunCommand(cmd);

But result is in BsonDocument to get the correct result you can use this methods:

var intResult = result["retval"].ToInt32();

2 Comments

when the function return a number it works fine. Now i want to try to return a document's list. i changed test_function to this: function (x,y) { return db.MyDB.find({_id:1}); } running into mongo shell it return one document. but from dotnet it does not work.
@FDB AFAIK you can't get results of database commands in this way in C#, please ask another question for that and I hope someone answers it ;).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.