Getting Started With Stored Procedures in MongoDB

Posted in Databases on Thursday, the 26th of August, 2010.

My most exciting technical discovery this year has been MongoDB. Mongo is proving to be a very satisfying solution to a lot of problems, so I'll no doubt be mentioning it here many times in the near future.

MongoDB has support for stored procedures, which is great, but as a newcomer, I found it wasn't entirely obvious how to get started. I also found that the typically quite excellent MongoDB documentation tended to fall short on examples of the specific things I had trouble with. So here's a quick writeup of getting stored procs up-and-running in MongoDB.

Comments

Posted by Projapati on Tuesday, the 5th of April, 2011.

Looks like you are putting entire sproc\js fun code when calling the function. It might look good with you 1 line fn, what about 500 lines js fn? How should it be done for that?

Posted by Chris on Tuesday, the 10th of May, 2011.

Nice post. Thanks for your contribution it was just what I was looking for.

Posted by jim on Sunday, the 27th of May, 2012.

Thanks for the article. I agree that one of the most powerful features of MongoDB is the fact that it uses javascript as its query and stored procedure language. This opens up the possibility of allowing developers to write stored procedures rather than request them from the DBA's. Of course there are some things that really should be done in a relational database, but mongodb solves many common problems.

Posted by lenny on Saturday, the 14th of July, 2012.

Very helpful! Thanks for posting

Posted by Aaron Heckmann on Tuesday, the 29th of January, 2013.

Heads up: MongoDB 2.4 changes the javascript engine from Spidermonkey to v8, so Spidermonkey only features like E4X and Iterators will not be available.

Posted by yujie on Tuesday, the 5th of February, 2013.

mongodb procedure has a return type?

Posted by Abdul Manaf C J on Monday, the 18th of April, 2016.

Hi,
i need to create stored procedure , that return a bulk data .

eg: db.system.js.save({_id:"sample2", value:function(fromDate, toDate){return db.samplecollection.find({"userid" : "manaf"})}});

how it will be possible?

Enter your comment: