Created
March 19, 2015 13:47
-
-
Save makeittotop/02dfc17a750b10187a8c to your computer and use it in GitHub Desktop.
A special system collection named system.js, can store JavaScript functions for reuse. Made use of this feature to implement the auto-increment feature found in traditional rdbms.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# To store a function use the db.collection.save() method | |
db.system.js.save( | |
{ | |
_id: "foo_bar", | |
value : function(x, y) { return x * y; } | |
} | |
) | |
db.system.js.save( | |
{ | |
_id : "bar_buzz" , | |
value : function (x, y){ return x * y; } | |
} | |
); | |
# Fire up the mongo shell | |
db.loadServerScripts(); | |
foo_bar(9, 8); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment