How to call function from Mongodb like "db.eval()"?


#1

I created the hi() function in Mongodb and i can execute it by db.eval("hi()").
how to execute on Meteor?


#2

Hi theara -

I’m sorry, I don’t have an answer, but was wondering what you were trying to do, and was going to suggest you keep the JS in Meteor, not in Mongo.

As Mongo themselves point out, there are several good reasons for not using javascript inside Mongo.
First, there are performance issues.
Second, it’s better to keep all your code under the same version control, rather than fragmenting it (which you might already be doing).

But personally, I was wondering why you’d use a javascript engine, to call a database call…to run javascript. Why not just run the JS in Meteor, where it is optimized by node, and let Mongo handle the DB side, which IT is optimized to do?

Just curious…good luck!
Aaron


#3

I think that it is fast, if we create store procedure or function in db like in MySQL.


#4

Hi theara -

Again, MongoDB does not have Stored Procedures. All it lets you do is run javascript on the Mongo server side.

There are many things stored procedures offer that you do not get with Mongo. For example:

  • cached execution plan
  • permissions management

But there are several benefits that both SProcs and embedded JS share - if you use it right.

  • Reduced network traffic (if you can do some filtering down on the server before the results hit the wire, maybe? Though I’m not sure why it would be any faster than a well-written query from the Meteor server)
  • Localizing the code (changes do not require a hot code push or restart of the server, and it’s easier to analyze the JS on the server to find missing indices, poorly optimized code, etc.)
  • protection against injection attacks (have there been MongoInjection attacks in the wild?)

So, if you’re coming from the world of SQL development and you are just looking for the ‘equivalent’ of sprocs - there aren’t any. It’s not quite that straightforward. Embedded JS in Mongo can help make things faster, but it’s not a ‘given’, and you should make an informed choice as to whether it’s the right solution for your app. I’d be interested to see benchmarks on queries run in the MongoDB (which is slower at running JS) vs from the JS in a Meteor Server. @arunoda, or anyone, have you seen anything like that?

I hope this information helps guide you to the best solution for your needs.

See this SO post for more.