How do I run a javascript script via the .NET driver

217 views
Skip to first unread message

woaksie

unread,
Apr 10, 2013, 6:08:51 PM4/10/13
to mongodb...@googlegroups.com

I want to be able to run a multi-step javascript mongo script via the .NET driver. What is the best way to do this?


Robert Stam

unread,
Apr 10, 2013, 10:43:17 PM4/10/13
to mongodb...@googlegroups.com
I assume by multi-step you mean more than one javascript statement.

Here's some sample code:

var script = "x = 1; y = 2; return x + y";
var result = database.Eval(script);
Console.WriteLine(result.ToInt32());

The return value of Eval is of type BsonValue, so it could be any JavaScript value. For example, the following returns a BsonDocument instead:

var script = "x = 1; y = 2; return { x : x, y : y, sum : x + y }";
var result = database.Eval(script);
Console.WriteLine(result.ToJson());

And the output of Console.WriteLine is:

{ "x" : 1.0, "y" : 2.0, "sum" : 3.0 }

I wouldn't recommend you use Javascript at the server too much. If you can simply fetch the data you need and manipulate it client side that is usually better.


On Wed, Apr 10, 2013 at 6:08 PM, woaksie <woa...@gmail.com> wrote:

I want to be able to run a multi-step javascript mongo script via the .NET driver. What is the best way to do this?


--
You received this message because you are subscribed to the Google Groups "mongodb-csharp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-cshar...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

John Woakes

unread,
Apr 11, 2013, 12:15:56 PM4/11/13
to mongodb-csharp
That is what I want to do.

I want to be able to upload a script to a service and get it to run it on its mongo database. I don't really know of an easy way to upload C# code with out deploying the whole service which seems overkill. The mongo database is not accessible from outside the service. I would use this for sorting out and fixing problems.

Daniil Monin

unread,
May 22, 2013, 4:39:49 PM5/22/13
to mongodb...@googlegroups.com
Hello, Robert.

Could you help me to understand, how I can run such as scripts like "db.Foo.Find()" via .Net driver?

Sometimes I need behavior like mongo shell with full support of remove/update/etc. scripts.

I have already tried something like this:

            var script = "return db.QUQU.find();";

            var res = dbReal.Eval(script);

But have no luck with it. It returns something like this: { "_mongo" : { "slaveOk" : false, "host" : "EMBEDDED" }, "_db" : { "_mongo" : { "slaveOk" : false, "host" : "EMBEDDED" }, "_name" : "simple" }, "_collection" : { "_mongo" : { "slaveOk" : false, "host" : "EMBEDDED" },....


Thanks in advance!

craiggwilson

unread,
May 22, 2013, 4:54:35 PM5/22/13
to mongodb...@googlegroups.com
Hi Daniil,
  I'd suggest you start by reading the documentation here: http://docs.mongodb.org/ecosystem/drivers/csharp/.  In particular, the Getting Started tutorial should answer your questions (http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-csharp-driver/#getting-started-with-csharp-driver).
Message has been deleted

Daniil Monin

unread,
May 22, 2013, 5:22:10 PM5/22/13
to mongodb...@googlegroups.com
Thank you craiggwilson, but I suppose my question is not correct.

I'll try to be more specific. 

For example, I develop some application where the user can enter script in text box (or something like), such as "db.Foo.Find()" hit F5 and it will return result, like in SQL Server Management Studio or mongo shell. It's should be dynamic script, and that's why I can't use something like this:

            var query = Query<Entity>.EQ(e => e.Id, id);
            entity = collection.FindOne(query); 

In getting started I can't find any solution for my question.

craiggwilson

unread,
May 22, 2013, 6:02:00 PM5/22/13
to mongodb...@googlegroups.com
The Query class has a method called Where.  You can pass in arbitrary javascript for evaluation on the server.  You can see the server documentation on this here: http://docs.mongodb.org/manual/reference/operator/where/.

PLEASE take not of all the caveats specified on that page.  In addition, you are also passing unverified code to the server.  This would be akin to a SQL injection attack.

Perhaps you should consider DynamicLinq (http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx) or an actual scripting engine instead.

Daniil Monin

unread,
May 22, 2013, 6:20:36 PM5/22/13
to mongodb...@googlegroups.com
Thank you for your help, craiggwilson! :)
Reply all
Reply to author
Forward
0 new messages