{"id":6016,"date":"2012-06-29T21:09:19","date_gmt":"2012-06-29T15:39:19","guid":{"rendered":"https:\/\/2thenew.xyz\/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:\/\/2thenew.xyz\/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=\"https:\/\/2thenew.xyz\/mean-stack-web-development-consulting\">MongoDB<\/a> uses <a title=\"react.js development\" href=\"https:\/\/2thenew.xyz\/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":58,"footnotes":""},"categories":[1185],"tags":[4260,4262,855,4846],"class_list":["post-6016","post","type-post","status-publish","format-standard","hentry","category-node-js-2","tag-mean-development","tag-mean-stack-development","tag-mongo-script","tag-mongodb"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"MongoDB uses javascript interface for the shell commands. We can really use this feature in a great way - 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(&#039;===== My Demo Script =====&#039;); print(db.getCollectionNames());\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Salil\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.xyz\/blog\/executing-mongodb-shell-commands-from-javascript-file\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Executing MongoDB shell commands from Javascript file | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"MongoDB uses javascript interface for the shell commands. We can really use this feature in a great way - 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(&#039;===== My Demo Script =====&#039;); print(db.getCollectionNames());\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.xyz\/blog\/executing-mongodb-shell-commands-from-javascript-file\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Executing MongoDB shell commands from Javascript file | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"MongoDB uses javascript interface for the shell commands. We can really use this feature in a great way - 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(&#039;===== My Demo Script =====&#039;); print(db.getCollectionNames());\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/executing-mongodb-shell-commands-from-javascript-file\\\/#article\",\"name\":\"Executing MongoDB shell commands from Javascript file | TO THE NEW Blog\",\"headline\":\"Executing MongoDB shell commands from Javascript file\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2012-06-29T21:09:19+05:30\",\"dateModified\":\"2016-11-30T12:09:03+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":3,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/executing-mongodb-shell-commands-from-javascript-file\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/executing-mongodb-shell-commands-from-javascript-file\\\/#webpage\"},\"articleSection\":\"Node.js, MEAN development, MEAN Stack Development, Mongo Script, MongoDB\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/executing-mongodb-shell-commands-from-javascript-file\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"name\":\"Node.js\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"position\":2,\"name\":\"Node.js\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/executing-mongodb-shell-commands-from-javascript-file\\\/#listItem\",\"name\":\"Executing MongoDB shell commands from Javascript file\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/executing-mongodb-shell-commands-from-javascript-file\\\/#listItem\",\"position\":3,\"name\":\"Executing MongoDB shell commands from Javascript file\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"name\":\"Node.js\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/\",\"name\":\"Salil\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/executing-mongodb-shell-commands-from-javascript-file\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/97e216cba95b296d5909ad7b93d1c338d0fdad7b95410c9412b126a90f4735d9?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Salil\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/executing-mongodb-shell-commands-from-javascript-file\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/executing-mongodb-shell-commands-from-javascript-file\\\/\",\"name\":\"Executing MongoDB shell commands from Javascript file | TO THE NEW Blog\",\"description\":\"MongoDB uses javascript interface for the shell commands. We can really use this feature in a great way - 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('===== My Demo Script ====='); print(db.getCollectionNames());\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/executing-mongodb-shell-commands-from-javascript-file\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/#author\"},\"datePublished\":\"2012-06-29T21:09:19+05:30\",\"dateModified\":\"2016-11-30T12:09:03+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Executing MongoDB shell commands from Javascript file | TO THE NEW Blog","description":"MongoDB uses javascript interface for the shell commands. We can really use this feature in a great way - 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('===== My Demo Script ====='); print(db.getCollectionNames());","canonical_url":"https:\/\/2thenew.xyz\/blog\/executing-mongodb-shell-commands-from-javascript-file\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.xyz\/blog\/executing-mongodb-shell-commands-from-javascript-file\/#article","name":"Executing MongoDB shell commands from Javascript file | TO THE NEW Blog","headline":"Executing MongoDB shell commands from Javascript file","author":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/salil\/#author"},"publisher":{"@id":"https:\/\/2thenew.xyz\/blog\/#organization"},"datePublished":"2012-06-29T21:09:19+05:30","dateModified":"2016-11-30T12:09:03+05:30","inLanguage":"en-US","commentCount":3,"mainEntityOfPage":{"@id":"https:\/\/2thenew.xyz\/blog\/executing-mongodb-shell-commands-from-javascript-file\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.xyz\/blog\/executing-mongodb-shell-commands-from-javascript-file\/#webpage"},"articleSection":"Node.js, MEAN development, MEAN Stack Development, Mongo Script, MongoDB"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.xyz\/blog\/executing-mongodb-shell-commands-from-javascript-file\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.xyz\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/node-js-2\/#listItem","name":"Node.js"}},{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/node-js-2\/#listItem","position":2,"name":"Node.js","item":"https:\/\/2thenew.xyz\/blog\/category\/node-js-2\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/executing-mongodb-shell-commands-from-javascript-file\/#listItem","name":"Executing MongoDB shell commands from Javascript file"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/executing-mongodb-shell-commands-from-javascript-file\/#listItem","position":3,"name":"Executing MongoDB shell commands from Javascript file","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/node-js-2\/#listItem","name":"Node.js"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.xyz\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.xyz\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.xyz\/blog\/author\/salil\/#author","url":"https:\/\/2thenew.xyz\/blog\/author\/salil\/","name":"Salil","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.xyz\/blog\/executing-mongodb-shell-commands-from-javascript-file\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/97e216cba95b296d5909ad7b93d1c338d0fdad7b95410c9412b126a90f4735d9?s=96&d=mm&r=g","width":96,"height":96,"caption":"Salil"}},{"@type":"WebPage","@id":"https:\/\/2thenew.xyz\/blog\/executing-mongodb-shell-commands-from-javascript-file\/#webpage","url":"https:\/\/2thenew.xyz\/blog\/executing-mongodb-shell-commands-from-javascript-file\/","name":"Executing MongoDB shell commands from Javascript file | TO THE NEW Blog","description":"MongoDB uses javascript interface for the shell commands. We can really use this feature in a great way - 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('===== My Demo Script ====='); print(db.getCollectionNames());","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.xyz\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.xyz\/blog\/executing-mongodb-shell-commands-from-javascript-file\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/salil\/#author"},"creator":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/salil\/#author"},"datePublished":"2012-06-29T21:09:19+05:30","dateModified":"2016-11-30T12:09:03+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.xyz\/blog\/#website","url":"https:\/\/2thenew.xyz\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.xyz\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Executing MongoDB shell commands from Javascript file | TO THE NEW Blog","og:description":"MongoDB uses javascript interface for the shell commands. We can really use this feature in a great way - 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('===== My Demo Script ====='); print(db.getCollectionNames());","og:url":"https:\/\/2thenew.xyz\/blog\/executing-mongodb-shell-commands-from-javascript-file\/","og:image":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Executing MongoDB shell commands from Javascript file | TO THE NEW Blog","twitter:description":"MongoDB uses javascript interface for the shell commands. We can really use this feature in a great way - 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('===== My Demo Script ====='); print(db.getCollectionNames());","twitter:image":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"6016","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":"","og_description":"","og_object_type":"blog","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":"","og_article_tags":"","twitter_use_og":false,"twitter_card":"summary","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-29 14:19:05","updated":"2024-02-29 09:07:15","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.xyz\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.xyz\/blog\/category\/node-js-2\/\" title=\"Node.js\">Node.js<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tExecuting MongoDB shell commands from Javascript file\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.xyz\/blog"},{"label":"Node.js","link":"https:\/\/2thenew.xyz\/blog\/category\/node-js-2\/"},{"label":"Executing MongoDB shell commands from Javascript file","link":"https:\/\/2thenew.xyz\/blog\/executing-mongodb-shell-commands-from-javascript-file\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts\/6016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/comments?post=6016"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts\/6016\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/media?parent=6016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/categories?post=6016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/tags?post=6016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}