[SERVER-1536] Mongodb Shell: support for using it as a interpreter Created: 02/Aug/10  Updated: 12/Jul/16  Resolved: 11/Aug/10

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: 1.7.0

Type: New Feature Priority: Trivial - P5
Reporter: Vilhelm K. Vardøy Assignee: Mathias Stearn
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

I would love the possibility to do something like this:

#!/usr/bin/mongo --interpreter
db = connect("mongo.machine.host.com/mydatabase")
[...]

Doing this currently results in this (because of the first line which starts on #):
$ ./test.mongo.js
Mon Aug 2 14:59:54 SyntaxError: illegal character ./test.mongo.js:0
failed to load: ./test.mongo.js

For now, I have created a wrapper in bash to simulate the functionality (/usr/bin/mongointerpreter):
#!/bin/bash
file=`tempfile`.js
cat $1 | grep -v '^#' > $file
mongo --quiet $file
rm -f $file

Usage:
#!/usr/bin/mongointerpreter
db = connect("mongo.machine.host.com/mydatabase");
print(db.sessions.find().count());

I could probably manage to create a patch within the week if the feature sounds reasonable for you (a.k.a. if there's interest for it).



 Comments   
Comment by auto [ 11/Aug/10 ]

Author:

{'login': 'RedBeard0531', 'name': 'Mathias Stearn', 'email': 'mathias@10gen.com'}

Message: ignore first line if starts with shebang SERVER-1536
http://github.com/mongodb/mongo/commit/3fcfa3ba16bffa681a21e863b270b5cf31797472

Comment by Vilhelm K. Vardøy [ 02/Aug/10 ]

Did an improvement of the mongointerpreter-wrapper (adding support for arguments), if anyone should find it useful:

#!/bin/bash
file=`tempfile`.js
cat $1 | grep -v '^#' > $file
ARGS="var arguments=["
while [ ! -z "$1" ]; do
ARGS="$ARGS '$1'"
if [ ! -z $2 ]; then
ARGS="$ARGS,"
fi
shift
done
ARGS="$ARGS];"
mongo --quiet --eval "$ARGS" $file
rm -f $file

Basically creates a array called arguments which is accessible from the script.

Generated at Thu Feb 08 02:57:18 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.