0

I'm trying to make a "wrapper" bash alias function to evaluate mongo snippets

alias mongoshelleval=mongoshelleval
mongoshelleval(){
    mongo --verbose --eval $1 $db
    }

And I have the snippets stored as env variables

export delsessions='"db.sessions.drop()"'

The I execute the thing

mongoshelleval $delsessions

but it doesn't carry out the operation. It just

$ mongoshelleval $delsessions
MongoDB shell version: 2.4.8
Thu Aug 07 03:50:18.809 versionArrayTest passed
connecting to: db
Thu Aug 07 03:50:18.861 creating new connection to:127.0.0.1:27017
Thu Aug 07 03:50:18.867 BackgroundJob starting: ConnectBG
Thu Aug 07 03:50:18.870 connected connection!
db.sessions.drop()
Thu Aug 07 03:50:18.880 freeing 1 uncollected class mongo::DBClientWithCommands objects

Without throwing any errors. Just prints back the argument db.sessions.drop()

And the sessions are still there

$ mongoshell
MongoDB shell version: 2.4.8
connecting to: db
db.sessions.find()
{ "_id" : "yr5rxguSbb32Q880Jj36Rq2_uU
{ "_id" : "9PQh_ml5Gloiaunv6pbgVevM_6
{ "_id" : "OacGUyf-V1DcTQIg3lMgFXUL-N
{ "_id" : "GC7gh09iqVUny9HM8gnGl9Hzxt
{ "_id" : "_f4koE0tagJ7vwuU76BatcDeEb
{ "_id" : "t1CX_eqi7FcCREfBB2X5CpgOcz

Why doesn't it do what I intend it to do, and why isn't it throwing any errors if it's not carrying out the query?

laggingreflex
  • 32,948
  • 35
  • 141
  • 196

1 Answers1

0

You have an extra pair of quotes. Try

export delsessions="db.sessions.drop()"

For example, the following works for me

mongo> db.sessions.find()
{ "_id" : ObjectId("53e5078d5b8b40be16223165"), "a" : 1 }
mongo> exit
bye

bash$ export test="db.sessions.drop()"
bash$ mongo --eval $test
MongoDB shell version: 2.6.3
connecting to: test
true
bash$ mongo

mongo> db.sessions.find()
// nothing

I augmented my > and $ prompts with whether I was in the mongo shell or the bash shell, for extra clarity.

wdberkeley
  • 11,531
  • 1
  • 28
  • 23