{"id":598,"date":"2017-07-13T19:07:27","date_gmt":"2017-07-13T18:07:27","guid":{"rendered":"http:\/\/www.psinke.nl\/blog\/?p=598"},"modified":"2017-07-13T19:07:27","modified_gmt":"2017-07-13T18:07:27","slug":"creating-javascript-functions-in-a-mongodb-database","status":"publish","type":"post","link":"https:\/\/www.psinke.nl\/blog\/creating-javascript-functions-in-a-mongodb-database\/","title":{"rendered":"creating Javascript functions in a MongoDB database"},"content":{"rendered":"<div class=\"afa2b3b851443650502c43e85c746c1f\" data-index=\"1\" style=\"float: none; margin:0px;\">\n<script type=\"text\/javascript\"><!--\r\ngoogle_ad_client = \"ca-pub-4223201538046844\";\r\n\/* standaardblok *\/\r\ngoogle_ad_slot = \"9522527129\";\r\ngoogle_ad_width = 320;\r\ngoogle_ad_height = 50;\r\n\/\/-->\r\n<\/script>\r\n<script type=\"text\/javascript\"\r\nsrc=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\">\r\n<\/script>\n<\/div>\n<p>Although it&#8217;s not recommended to store your application logic in MongoDB, it&#8217;s sometimes useful to create Javascript functions for database management purposes in the database.<\/p>\n<p>The process is really easy. MongoDB has a special collection named <strong>system.js<\/strong>, that can contain Javascript functions. Creating a function in MongoDB is as simple as inserting a record with the name and the function code into the fields named <strong>_id<\/strong> and <strong>value<\/strong> in this collection.<\/p>\n<p>Example:<\/p>\n<div class=\"highlight-javascript\">\n<div class=\"highlight\">\n<pre><span class=\"nx\">db<\/span><span class=\"p\">.<\/span><span class=\"nx\">system<\/span><span class=\"p\">.<\/span><span class=\"nx\">js<\/span><span class=\"p\">.<\/span><span class=\"nx\">save<\/span><span class=\"p\">(<\/span>\r\n  <span class=\"p\">{<\/span>\r\n    <span class=\"nx\">_id<\/span> <span class=\"o\">:<\/span> <span class=\"s2\">\"myFirstFunction\"<\/span> <span class=\"p\">,<\/span>\r\n    <span class=\"nx\">value<\/span> <span class=\"o\">:<\/span> <span class=\"kd\">function<\/span> <span class=\"p\">(a<\/span><span class=\"p\">,<\/span> b<span class=\"p\">){<\/span> <span class=\"k\">return<\/span> a <span class=\"o\">+<\/span> b<span class=\"p\">;<\/span> <span class=\"p\">}<\/span>\r\n  <span class=\"p\">}<\/span>\r\n<span class=\"p\">);<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>The function is now accessible in any Javascript context (for instance in a $where). If you need to access the functions from the MongoDB shell, call the command db.loadServerScripts() to load them for the current database.<\/p>\n<p>&nbsp;<\/p>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Although it&#8217;s not recommended to store your application logic in MongoDB, it&#8217;s sometimes useful to create Javascript functions for database management purposes in the database. The process is really easy. MongoDB has a special collection named system.js, that can contain Javascript functions. Creating a function in MongoDB is as simple as inserting a record with&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[366,121,192],"tags":[465,142,431,466],"class_list":["post-598","post","type-post","status-publish","format-standard","hentry","category-fast-data-big-data","category-tips-and-tricks","category-web-software-development","tag-functions","tag-javascript","tag-mongodb","tag-server"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/posts\/598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/comments?post=598"}],"version-history":[{"count":1,"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/posts\/598\/revisions"}],"predecessor-version":[{"id":599,"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/posts\/598\/revisions\/599"}],"wp:attachment":[{"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/media?parent=598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/categories?post=598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/tags?post=598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}