13

I have questions regarding stored JavaScript Procedures. After reading the Blog Entry from PointBeing, I have some questions.

  • Is there an advantage to storing my code in the DB? I mean functions like lookups for documents, not adding numbers like the example from PointBeing.
  • Is MongoDB stored javascript faster than node.js javascript?
  • Is MongoDB stored javascript queries cached and are they any faster?

I'm interested in MongoDB stored javascript performance compared to Node.js Javascript.

4
  • 2
    Keep one question per post. SO does not allow to ask 4 questions simultaneously. May 8, 2015 at 7:16
  • 3
    @Salvador These questions are all closely related, I see little point in breaking them up into three separate posts.
    – deceze
    May 8, 2015 at 7:25
  • 1
    1: No, 2: No, 3: Of course not
    – Sammaye
    May 8, 2015 at 7:29
  • this prevents people from answering your question What if I know the answer to only 2 questions, or only to one. Moreover the the first question is subjective and ambiguous. A real advantage depends on many factors and as majority of the things in development there is no advantage, there are tradeoffs. May 8, 2015 at 7:29

2 Answers 2

18

Evaluating functions stored in db.system.js ("Stored procedures", when you would like to call them that) is deprecated. The articles on the db.eval shell function and the eval database command have a "Deprecated since version 3.0" warning and the article on server-sided javascript doesn't mention it anymore. So you should avoid using it. One reason is that you can not run a javascript function when you use sharding. So when you build an application which requires eval, you prevent it from scaling in the future. Another is that javascript functions undermine the permission concept. They always need to be run as admin, which makes it impossible to establish a sane permission system. This is especially problematic from a security standpoint considering that server-sided scripts which use user-provided data can potentially be vulnerable to arbitrary script injections.

The advantage of server-sided javascript is that it runs on the database server. This reduces latency between application server and database server when you need to perform a large number of queries. But you can get the same advantage by opening a mongo shell on the database server and executing it there.

The latency advantage is only relevant when you perform multiple queries from your script. When you have only one query, you will still have the latency when invoking the script. So you gain nothing except unnecessary complexity.

There is no additional caching or other optimization for server-sided javascript. Even worse: It will get reparsed and reinterpreted everytime you run it. So it might even be slower than javascript in your application server.

Further, many complex queries which would require script support to implement only with find() can often be expressed using aggregation which will in most cases be far faster than doing the same with find() and javascript because the aggregation framework is implemented in C++ and has access to the raw BSON documents.

5
  • It's difficult implement queries using the MongoDB C# Driver Version 1.7.0.4714 API when the query involves a lot of quasi-joins between MongoDB Collections. I believe it slows performance if you bring in large amount of data from MongoDB world into C# world as POCO objects in a list. So, I was thinking if it would be good to adopt the implementation practice of using C# to invoke stored JavaScript in MongoDB. You are saying invoking Stored JavaScript is bad. Please see: stackoverflow.com/questions/36220272/…
    – CS Lewis
    Mar 26, 2016 at 12:43
  • @CSLewis When you need to do lots of quasi-joins, then it is very likely that you are using mongodb like a relational database, which rarely gets you good results.
    – Philipp
    Mar 26, 2016 at 12:50
  • ok, but, what if I just do an intensive amount of analysis on a collection with a large amount of data. I think it would be slow performance by bringing in a lot of documents from the collection as C# POCO objects in the C# world. Instead, I thought it would be better if C# invoked Stored JavaScript which would do a lot of the intensive analysis, and then return the ending results to the C# world. What other alternative do I have?
    – CS Lewis
    Mar 26, 2016 at 13:03
  • @CSLewis I have built a system with MongoDB that does joins in C# code between the results of many queries, with several thousand records at a time. Not really what MongoDB was intended for but using the aggregation pipeline it can be done effectively, and grants access to the scalability of mongo without paying for expensive SQL servers. The key is having the correct indexes and making sure they get used in your queries, and then making sure the C# code that processes the records is efficient (use dictionaries instead of lists for lookup, etc).
    – zephos2014
    Aug 13, 2018 at 16:07
  • @Philipp Thanks for this answer. It cleared up some misconceptions I had about MongoDB.
    – zephos2014
    Aug 13, 2018 at 16:09
4

The hilarious thing is that blog post ( http://pointbeing.net/weblog/2010/08/getting-started-with-stored-procedures-in-mongodb.html ) was written when JS only took single threaded global lock.

That means there was no con-currency features or more granular lock associated with it (the lock still being a problem and con-currency is only achieved through multiple isolates still). Just because you see it in some random blog post does not mean it should be used.

To answer your questions directly:

  1. Nope. In fact the disadvantage is that the calling user needs full admin rights. This means you give every single privilege to your web user since the inbuilt JS enigne has hooks for everything, including administration functions as such it requires admin rights in order to run.

  2. Calling JS from JS to JS to C++ in JS? No

  3. No, MongoDB caching does not work like that. I recommend you read the fundamentals documentation: http://docs.mongodb.org/manual/faq/fundamentals/

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.