Skip to content

Instantly share code, notes, and snippets.

@makeittotop
Created March 19, 2015 13:47
Show Gist options
  • Save makeittotop/02dfc17a750b10187a8c to your computer and use it in GitHub Desktop.
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.
# 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