How to execute mongo commands through shell script

2019-01-03 11:19发布

I want to execute mongo commands in shell script.

I tried following way test.sh

#!/bin/sh

mongo myDbName

db.mycollection.findOne()

show collections

When I execute above script ./test.sh

Then mongo connection established but next commands not executed

How to execute other commands through sh script [test.sh] ?

Please help me

标签: shell sh mongodb
19条回答
家丑人穷心不美
2楼-- · 2019-01-03 12:13

I use the "heredoc" syntax, which David Young mentions. But there is a catch:

#!/usr/bin/sh

mongo <db> <<EOF
db.<collection>.find({
  fieldName: { $exists: true }
})
.forEach( printjson );
EOF

The above will NOT work, because the phrase "$exists" will be seen by the shell and substituted with the value of the environment variable named "exists." Which, likely, doesn't exist, so after shell expansion, it becomes:

#!/usr/bin/sh

mongo <db> <<EOF
db.<collection>.find({
  fieldName: { : true }
})
.forEach( printjson );
EOF

In order to have it pass through you have two options. One is ugly, one is quite nice. First, the ugly one: escape the $ signs:

#!/usr/bin/sh

mongo <db> <<EOF
db.<collection>.find({
  fieldName: { \$exists: true }
})
.forEach( printjson );
EOF

I do NOT recommend this, because it is easy to forget to escape.

The other option is to escape the EOF, like this:

#!/usr/bin/sh

mongo <db> <<\EOF
db.<collection>.find({
  fieldName: { $exists: true }
})
.forEach( printjson );
EOF

Now, you can put all the dollar signs you want in your heredoc, and the dollar signs are ignored. The down side: That doesn't work if you need to put shell parameters/variables in your mongo script.

Another option you can play with is to mess with your shebang. For example,

#!/bin/env mongo
<some mongo stuff>

There are several problems with this solution:

  1. It only works if you are trying to make a mongo shell script executable from the command line. You can't mix regular shell commands with mongo shell commands. And all you save by doing so is not having to type "mongo" on the command line... (reason enough, of course)

  2. It functions exactly like "mongo <some-js-file>" which means it does not let you use the "use <db>" command.

I have tried adding the database name to the shebang, which you would think would work. Unfortunately, the way the system processes the shebang line, everything after the first space is passed as a single parameter (as if quoted) to the env command, and env fails to find and run it.

Instead, you have to embed the database change within the script itself, like so:

#!/bin/env mongo
db = db.getSiblingDB('<db>');
<your script>

As with anything in life, "there is more than one way to do it!"

查看更多
The star\"
3楼-- · 2019-01-03 12:14

As suggested by theTuxRacer, you can use the eval command, for those who are missing it like I was, you can also add in your db name if you are not trying to preform operation on the default db.

mongo <dbname> --eval "printjson(db.something.find())"
查看更多
你好瞎i
4楼-- · 2019-01-03 12:16

How about this:

echo "db.mycollection.findOne()" | mongo myDbName
echo "show collections" | mongo myDbName
查看更多
仙女界的扛把子
5楼-- · 2019-01-03 12:17

Put your mongo script into a .js file.

Then execute mongo < yourFile.js

Ex:

demo.js //file has your script

use sample  //db name
show collections

keep this file in "c:\db-scripts"

Then in cmd prompt go to "c:\db-scripts"

C:\db-scripts>mongo < demo.js

This will execute the code in mongo and shows the output

C:\db-scripts>mongo < demo.js
Mongo shell version: 3.0.4
Connecting to: test
switched to db sample
users   //collection name
tasks   //collection name
bye
C:\db-scripts>
查看更多
Animai°情兽
6楼-- · 2019-01-03 12:18

There is an official documentation page about this as well.

Examples from that page include:

mongo server:27017/dbname --quiet my_commands.js
mongo test --eval "printjson(db.getCollectionNames())"
查看更多
手持菜刀,她持情操
7楼-- · 2019-01-03 12:19
mongo <<EOF
use <db_name>
db.getCollection("<collection_name>").find({})
EOF
查看更多
登录 后发表回答