{"id":6016,"date":"2012-06-29T21:09:19","date_gmt":"2012-06-29T15:39:19","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6016"},"modified":"2016-11-30T12:09:03","modified_gmt":"2016-11-30T06:39:03","slug":"executing-mongodb-shell-commands-from-javascript-file","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/executing-mongodb-shell-commands-from-javascript-file\/","title":{"rendered":"Executing MongoDB shell commands from Javascript file"},"content":{"rendered":"<p><a title=\"MEAN web development\" href=\"http:\/\/www.tothenew.com\/mean-stack-web-development-consulting\">MongoDB<\/a> uses <a title=\"react.js development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">javascript interface<\/a> for the shell commands. We can really use this feature in a great way &#8211; by writing the MongoDB (shell) commands in a javascript (.js file) and execute in single go.<\/p>\n<p>In this blog, I am going to show an example for the same:<\/p>\n<p>[javascript]<br \/>\n  print(&#8216;===== My Demo Script =====&#8217;);<br \/>\n  print(db.getCollectionNames()); \/\/ prints all collection names in respective database<br \/>\n  print(db.myCollection.count()); \/\/ prints the count of myCollection collection.<br \/>\n[\/javascript]<\/p>\n<p><strong>shell command to execute the script<\/strong> &#8211;<\/p>\n<p>[shell]<br \/>\n  mongo 127.0.0.1\/my-profile-db &#8211;username=&#8217;username&#8217; &#8211;password=&#8217;pwd&#8217; mongodb-script.js<br \/>\n[\/shell]<\/p>\n<p><strong>mongodb-script.js<\/strong> is the shell script.<br \/>\nBasically this is just a regular mongodb connection command executing the script (after establishing successful connection).<\/p>\n<p>In my next blog I will discuss an\u00a0advance use-case &#8211; exporting collection records using shell script.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB uses javascript interface for the shell commands. We can really use this feature in a great way &#8211; by writing the MongoDB (shell) commands in a javascript (.js file) and execute in single go. In this blog, I am going to show an example for the same: [javascript] print(&#8216;===== My Demo Script =====&#8217;); print(db.getCollectionNames()); [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":22},"categories":[1185],"tags":[4260,4262,855,4846],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6016"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6016"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6016\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}