MongoDB supports the use of stored procedures, and its stored procedures are implemented in JavaScript, are present in the System.js table, can receive and output parameters, return the state value of executing stored procedures, or can be nested calls.
so I understand that the MongoDB stored procedure is :
The JavaScript variables are stored in a special collection of MongoDB databases: system.js tables, which can then be called in any MongoDB JavaScript context, including "$where" clauses, Db.eval calls, MapReduce job.
1. Adding a stored procedure
Db.system.js.save ({_id: Stored procedure name, value: Stored procedure body})
Or
Db.system.js.insert ({_id: Stored procedure name, value: Stored procedure body})
Where:the _id and the value properties are required, and if this property is not _id, it will cause an inability to invoke. You can also add other properties to describe the stored procedure. Like what:
Db.system.js.insert ({_id: Stored procedure name, value: Stored procedure body, Discrption: "This is a stored procedure"})
2. Modifying stored procedures
Db.system.js.update ({_id: Stored procedure name},{value: Stored procedure Body})
3. Execute the stored procedure
Db.eval (' Stored procedure name (parameter) ')
4. Find Stored Procedures
Db.system.js.find ();
The above commands can be executed under the MongoDB Shell Command window or written in a JavaScript file.
The following example is written in a JavaScript file.
Create a new test.js with the following code :
vardb = Connect (' school ');//The connected database name is: SchoolDb.courses.save ({name: "English", Time: ' Tuesday a.m. 8:00-9:50 '});d B.courses.save ({name:"Math", Time: ' Tuesday morning 10:00-12:00 '});d B.courses.save ({name:"Language", Time: ' Tuesday noon 2:00-4:00 '});d B.people.save ({name:' Liu Xiang ', type: ' Teacher ', age:28});d B.people.save ({name:' Xiaobianzi ', type: ' Student ', age:12});//1. Adding a stored procedure //Db.system.js.save ({_id: Stored procedure name, value: Stored procedure body})Db.system.js.save ({_id: "Getcoursescount", Value:function(){returnDb.courses.count ();},description: ' Get the number of courses '}); //Db.system.js.insert ({_id: Stored procedure name, value: Stored procedure body})Db.system.js.insert ({_id:"GetPeople", Value:function(type) {returntype; }, Description:' Show people by type ' });//2. Modifying stored Procedures //db.system.js.update ({_id: Stored procedure name},{value: Stored procedure body})db.system.js.update ({_id:"GetPeople"},{Value:function(type) {if(type==NULL){ returnDb.people.find (). ToArray (); }Else { returndb.people.find ({type:type}). ToArray (); } }});//3. Execute the stored procedure //db.eval (' Stored procedure name (parameter) ')varObj=db.eval ("GetPeople ()");p rint (json.stringify (obj));varObj1=db.eval ("GetPeople (' Student ')");p rint (Json.stringify (obj1));p rint (Db.eval ("Getcoursescount ()"));//4. Find Stored Procedures //Db.system.js.find ();varObj2=db.system.js.find ();p rint (Json.stringify (Obj2.toarray ()));
Locate the Test.js file location, my file location, and then hold down the SHIFT key, click the right mouse button to pop up a menu, select "Open Command Window here (W)"
Pop up the command window, and then run Test.js, there are two ways
Method 1:
Enter MONGO test.js in the Command window, press ENTER, Test.js will be run, as
Method 2: Press ENTER MONGO in the command window and enter load (' test.js ') so that the test.js will also be run, such as:
MongoDB Stored Procedures