MongoDB Stored Procedures

Source: Internet
Author: User
Tags mongodb query

Stored Procedures

The stored procedure for a relational database is described as a set of SQL statements to complete a particular function, compiled and stored in a database, and executed by the user by specifying the name of the stored procedure and giving the parameter (if the stored procedure has parameters).

MongoDB also has stored procedures, but MongoDB is written in JavaScript, which is the charm of MongoDB.

Saving stored procedures

MongoDB's stored procedure is stored in the Db.system.js table, let's start with a simple example:

1 function Add (x, y  ) {2     return x+y;   3 }  

Now we'll save this stored procedure to the table in Db.system.js:

Create stored procedure Code

1 > Db.system.js.save ({"_id": "Myadd", Value:functionreturn X+y;}});  

Where: _id and the Value property is required, if there is no _id this property, will cause later cannot be called (so far I have not found the way to call the method, if you have any way, please reply to me.) )。 You can add additional attributes to describe this stored procedure. Like what:

1 > Db.system.js.save ({"_id": "MyAdd1", Value:functionreturn x+y;}, "Discrption": "X is Number, and Y is number "});  

Added discrption to describe this function.

Querying stored Procedures

You can use Find to query stored procedures, as described in the previous MongoDB query document, for example:

Querying stored procedure Code

1 //querying all stored procedures directly2>Db.system.js.find (); 3{"_id": "Myadd", "value":function__cf__13__f__add (x, y) {4     returnX +y; 5 } }  6{"_id": "MyAdd1", "value":function__cf__14__f__add (x, y) {7     returnX +y; 8}, "Discrption": "X is number, and Y is number" }  9{"_id": ObjectId ("5343686ba6a21def9951af1c"), "value":function__cf__15__f__Ten Add (x, y) { One     returnX +y;  A } }   - //querying stored procedures that are _id as MyAdd1 -> Db.system.js.find ({"_id": "MyAdd1"});  the{"_id": "MyAdd1", "value":function__cf__16__f__add (x, y) { -     returnX +y;  -}, "Discrption": "X is number, and Y is number" }   ->
Executing stored procedures

How does a saved store execute?

Here's a fantastic function, eval; if the person who knows about JS definitely knows this eval. Used to execute a string (description of the relatively superficial, hehe), in MongoDB using Db.eval ("function name (parameter 1, parameter 2 ...)"), to execute the stored procedure (the function name is looking for the _id):

Execute stored procedure Code

1 > Db.eval (' Myadd ');   2

Eval will find the corresponding _id property to execute the stored procedure.

Db.eval () is a strange thing, and we can call the logic of the stored procedure directly inside and at the same time without having to declare the logic of the stored procedure beforehand.

Executing stored procedure Code

1 > Db.eval (function() {return 3+3;});   2 6  

MongoDB Stored Procedures

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.