3

how to run a Mongo db script on a remote server?

I know below command can be used for the same on local as mentioned here:How to execute mongo commands through shell scripts?

mongo < yourFile.js

I want to run this script on a remote server

mongodb:uri:mongodb://user:password@mongodb01d.mydomain.com:27017/mydb

shabinjo
  • 1,353
  • 1
  • 14
  • 18

2 Answers2

7

With Mongo on local machine :

 mongo -u <user> -p <password> mongodb01d.mydomain.com:27017/mydb <yourFile.js>
DamienzOnly
  • 88
  • 1
  • 9
tronic
  • 439
  • 2
  • 11
2

It might be a little bit off topic, but in case you want to / have to use Powershell for a lack of options, you can run:

(Get-Content yourFile.js) | & mongo.exe 'mongodb://user:password@mongodb01d.mydomain.com:27017/mydb'

or

"print('Hello');print('Hello')" | & mongo.exe 'mongodb://user:password@mongodb01d.mydomain.com:27017/mydb'

or

& 'mongo.exe' 'mongodb://user:password@mongodb01d.mydomain.com:27017/mydb' --eval "print('Hello');print('Hello')"

I had some difficulties with $regex, because Powershell interpreted it as a variable, so I had to use `$regex (with an additional backtick) instead.

B--rian
  • 5,011
  • 10
  • 34
  • 73
0815 163
  • 41
  • 3