Executing MongoDB shell commands from Javascript file « Intelligrape Groovy & Grails Blogs

Executing MongoDB shell commands from Javascript file

Posted by

As we know MongoDB uses javascript interface for the shell commands.
So, 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.
Here I gonna show a simple example.

 

  print('===== My Demo Script =====');
  print(db.getCollectionNames()); // prints all collection names in respective database
  print(db.myCollection.count()); // prints the count of myCollection collection.

 

shell command to execute the script -

  mongo 127.0.0.1/my-profile-db --username='username' --password='pwd' mongodb-script.js

 

mongodb-script.js is the shell script.
Basically this is just a regular mongodb connection command executing the script (after establishing successful connection).

 

In my next blog I will discuss little more advance use-case – exporting collection records using shell script.

 

I hope this might help someone.

 

Please put your comment, if there’s any better way out there.

 

Cheers!
Salil Kalia
Salil [at] IntelliGrape [dot] com
Twitter LinkedIn

This entry was posted on June 29th, 2012 at 9:09 pm and is filed under Grails . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Executing MongoDB shell commands from Javascript file”

  1. JMC says:

    Very helpful thank you !

Leave a Reply