Execute Mongo commands in Shell Script

1153200 pts.
Tags:
Big Data
MongoDB
I'm trying to execute MongoDB commands using Shell Script. Here's what I tried so far:
#!/bin/sh

mongo myDbName

db.mycollection.findOne()

show collections
When I executed it, it said the connection was established but not executed. Can someone help me out?
1

Answer Wiki

Thanks. We'll let you know when a new response is added.
Hi ITKE, 
You can also evaluate a command using the –eval flag, if it is just a single command.
mongo –eval “printjson(db.serverStatus())”  I hope it will assist you in resolving issues.
Thanks & Regards
Clark Kent

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • kyleBut
    Hi clark445kent,

    If its not a single command, what should we do?

    Is there any way to execute mongo commands in shell script except calling js file in script?
    0 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: