Stored procedures in mongo and db.runCommand privileges

3,276 views
Skip to first unread message

Kostya Remizov

unread,
Feb 27, 2014, 5:05:01 AM2/27/14
to mongod...@googlegroups.com
I'm trying  to create stored procedures mechanism in mongodb using system.js stored functions. But unfortunately user with "readWrite" role can't execute db.runCommand or db.eval methods.
What roles should I grant to user?
Example of code:
db.runCommand( {
     
eval:  'function(user) {return getUserInfo(user)};',
      args
: [ 1 ],
      nolock
: true
   
}
);
uncaught exception
: { "ok" : 0, "errmsg" : "unauthorized" }
or
db.eval('return getUserInfo(1);');
uncaught exception
: { "ok" : 0, "errmsg" : "unauthorized" }


Sam Millman

unread,
Feb 27, 2014, 9:25:21 AM2/27/14
to mongod...@googlegroups.com
Those are not stored procedures and are highly recommended not to bge used.

No you must have admin level to execute eval.


--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Kostya Remizov

unread,
Feb 28, 2014, 5:45:57 AM2/28/14
to mongod...@googlegroups.com

So what is the best analog of stored procedures in mongo?

четверг, 27 февраля 2014 г., 16:25:21 UTC+2 пользователь Sammaye написал:

Sam Millman

unread,
Feb 28, 2014, 6:53:13 AM2/28/14
to mongod...@googlegroups.com
Why do you need stored procedures in the first place?


Kostya Remizov

unread,
Feb 28, 2014, 7:10:18 AM2/28/14
to mongod...@googlegroups.com
I need to have some part of business logic on server. For example to execute several queries and do union or merge data. Or do the queries on several collections at a time and do something with the results. It's better from the performance perspective because you need o send less data between application and database. Actually there are many reasons why other databases have mechanism of stored procedures and packages.

пятница, 28 февраля 2014 г., 13:53:13 UTC+2 пользователь Sammaye написал:

s.molinari

unread,
Mar 1, 2014, 6:27:48 AM3/1/14
to mongod...@googlegroups.com
I asked a similar question and was told it isn't possible with Mongo, or rather, server side JS (in MongoDB) doesn't perform well and wasn't intended to be used as a stored procedure system.

Scott

s.molinari

unread,
Mar 1, 2014, 6:27:23 AM3/1/14
to mongod...@googlegroups.com

Asya Kamsky

unread,
Mar 2, 2014, 8:35:30 AM3/2/14
to mongodb-user
This is something that's not a good idea to do in MongoDB.

While there are many other databases that have this functionality, in those databases you usually normalize you data and many applications query for many different things.

With MongoDB you should structure the data the way your application needs it, and presumably that will reduce your need to obscure the business logic in server-side stored procedures.

Basically my suggestion is to keep all logic - application, business, etc. in your application.

Asya



Prithiviraj Kulasingham

unread,
Feb 23, 2015, 10:12:39 AM2/23/15
to mongod...@googlegroups.com
I too vote for this feature.  There are many advantages
1.  All developers don't have to master all the syntax and optimization methods of MongoDB. One person, who knows that better can write that piece of code.
2.  Monitoring is easy. Instead of searching for the similar where clauses and to manually grouping them in profiler data to determine the effective indexes, in one click it could be made easily. 
3.  Code level optimizations are easy. Instead of finding all places of code and modifying them, it could be done in one place.
4.  Reduces network traffic.

I understand the difficulties in implementing it at the current stage. But I recommend it as a must have for a future release.     

Asya Kamsky

unread,
Feb 25, 2015, 4:25:55 AM2/25/15
to mongodb-user
I don't think it is likely that this will ever make sense in MongoDB.    Which server would the stored procedures live/run on?   Primary?  Secondaries?  Which shard?  every shard?  Once you have more than a single server, the stored procedure model tends to break down somewhat.

Asya


For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.



--
{ "name" : "Asya Kamsky",
  "place" : [ "New York", "Palo Alto", "Everywhere else" ],
  "email" : "as...@mongodb.com",
  "blog" : "http://www.askasya.com/",
  "twitter": "@asya999" }

s.molinari

unread,
Feb 25, 2015, 5:17:01 AM2/25/15
to mongod...@googlegroups.com
Why not have stored procedures be a special system collection. If the cluster is only a replica set, then the mongo client is responsible for calling and running them. If the cluster is a shard, then the mongos is responsible for pulling the stored procedure and then running the queries to gather and assemble the data. Just a crazy idea, which I have no idea, if it has any merit or could work.

Scott
 

Kostya Remizov

unread,
Feb 25, 2015, 5:43:15 AM2/25/15
to mongod...@googlegroups.com
I think code should be available on each node and each shard with caching, like for example configs from config servers of shards.
It should be executed at the point where client is connected to - mongod if it's simple replica-set and mongos for sharded cluster. Mongos can execute code on each shard of the cluster for example like normal query.

I don't think that it has big difference from aggregation for example in implementation.

среда, 25 февраля 2015 г., 12:25:55 UTC+3 пользователь Asya Kamsky написал:

Kulasingham, Prithiviraj

unread,
Mar 3, 2015, 11:03:59 AM3/3/15
to mongod...@googlegroups.com
A MongoDB DBA may still need to understand the logic, to fine-tune databases. So, in NOSQL world the gap between developers and DBAs is reduced, but still we have not come to a stage where one role and complement the other role.  When the code is separated from parameters, it is easy to identify usage, parameter patterns and that will help DBA to improve the code.

Let me answer some of the questions/comments:
Question: Which server would the stored procedures live/run on?
A:  If an index can stay with the database, in all shards, in all secondaries and primary,  why can't a stored procedure stay?.  It should stay everywhere. The execution does not change. It is simply a replacement/ symbolic representation of actual code.
Question:  Why do you need stored procedures in the first place?
A:  For the same reason you need a query.  For the same reason you need methods in the application.

Comment: 
While there are many other databases that have this functionality, in those databases you usually normalize you data and many applications query for many different things.

Thinking that stored procedure has something to do with normalization has no base. Most of the database systems when they started did not have stored procedures. But they adopted it because they see the value of it.

Comment: With MongoDB you should structure the data the way your application needs it, and presumably that will reduce your need to obscure the business logic in server-side stored procedures.

If all applications will have data they way they want it, even though it may sound ideal, you only need CRUD operations. You dont even need "_id".   But the reality is far from it. That's why you have unique indexes, primary key, complex queries, multi-document updates and aggregates. 

If all should be kept in application, you don't need Ops Manager,  a pluggable storage engine etc. It shows something related to administration and not development. 
 
I am strongly for having stored procedures. 



For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/W4XUzVXxYFs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Best regards,

G.R. Prithiviraj Kulasingham

NoSQL Platform Services, Database Services

prithiviraj...@pearson.com
M: (+94) 776 316 936
T: (+94) 117 388 488   ext. 7106 (Desk), 7606 (Cell)
T: 1-877-238-7114   ext. 87106 (Desk), 87606 (Cell)

Connect with us!  http://DatabaseServices.cen.pearson.com/ 

sunil rawat

unread,
Aug 18, 2015, 8:58:56 AM8/18/15
to mongodb-user

Hi I also want to implemet Store procedure in my application, we are using mongoDb. Please see my code

//Saving Storeprocedure

           $mongo      = new \MongoClient();
            $database   = $mongo->selectDB('myDB');
            $collection = $database->selectCollection('system.js');
            $proccode = 'function getUsers() { db.Ledger.find({ "isDeleted": false });}';//Here I do not know how to make it dynamic to each time I call this Store procedure for diferent collections.
            $collection->save(
                            array(
                                '_id'   => 'getUsers',
                                'value' => new \MongoCode($proccode),
            ));

//Calling Store Procedure

               $toexec = 'function() { return getUsers() }';               
               $response = $database->execute($toexec);
              print_r( $response );//Not getting any record

    Please provide any solution what is wrong with this. We do not find good support for MongoDB we did great mistake by using this useless MongoDB.





 

Tim Hawkins

unread,
Aug 18, 2015, 9:34:48 AM8/18/15
to mongod...@googlegroups.com

You can do it yourself, just store each of your aggregation pipelines in a document in a collection, give it two elements, "name" and "pipeline", you could even call it "storedproceedures". they are afterall just data. And bobs your uncle. Surely you dont want to be running javascript in the server - sheeeeeeeeerrsh


On Wed, Feb 25, 2015, 18:17 s.molinari <scottam...@googlemail.com> wrote:
Why not have stored procedures be a special system collection. If the cluster is only a replica set, then the mongo client is responsible for calling and running them. If the cluster is a shard, then the mongos is responsible for pulling the stored procedure and then running the queries to gather and assemble the data. Just a crazy idea, which I have no idea, if it has any merit or could work.

Scott
 

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.

sam

unread,
Aug 18, 2015, 1:07:20 PM8/18/15
to mongodb-user
Stored procedures are out dated and should never be used.   They cause more problems then they solve. As for DBA vs Developer in Mongo that is a joke.  Mongo barely needs an admin as it is developer friendly.   And the comment that a developer doesn't need to know the syntax, then write a data access layer with specific method calls that can execute a query for the developer.    The problems listed in this post do not support having store procedure at all.


If you or your company use stored procedures maybe you should revisit your architecture. 
Reply all
Reply to author
Forward
0 new messages