mongo shell and the --eval

1,812 views
Skip to first unread message

Rick

unread,
Sep 6, 2010, 12:45:39 PM9/6/10
to mongodb-user
Hi guys..

Can anybody point me at the documentation for the mongo shell eval
command?

I'm trying to execute simple commands such as:

$ mongo dbname --eval "db.collectionname.find()"

This results in an error dbname.collectionname is not defined!!

It appears that db is mapped to the database name so the javascript
isnt being evaluated as normal shell commands

Any ideas?

Is there any documentation on this?

Eliot Horowitz

unread,
Sep 6, 2010, 1:04:53 PM9/6/10
to mongod...@googlegroups.com
Can you send the exact output?
What you're probably seeing is the DBQuery class output.

try:
mongo dbname --eval "db.foo.find().limit(10).forEach(printjson)"


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


rick....@gmail.com

unread,
Sep 6, 2010, 1:31:07 PM9/6/10
to mongod...@googlegroups.com
Well, for the example you gave of

mongo dbname --eval "db.foo.find().limit(10).
forEach(printjson)"

This worked (I added some test data)

If I just execute:

mongo dbname --eval "db.foo.find().limit(10)"

I get

MongoDB shell version: 1.4.4
url: core
connecting to: core
DBQuery: dbname.foo-> undefined

So.. do I always need to provide a means for the shell to write the output?

Eliot Horowitz

unread,
Sep 6, 2010, 1:32:38 PM9/6/10
to mongod...@googlegroups.com
Right now, it'll just print the .toString of the result.  In this case the .toString of the cursor object.
Perhaps we can do something smarter if its an eval.  
Want to open a jira case?

rick....@gmail.com

unread,
Sep 6, 2010, 1:34:41 PM9/6/10
to mongod...@googlegroups.com
Specifically, I want to write a set of scripts which will populate my database with data for use in my BDD tests

So I was expecting to be able to pass sets of insert statements etc i.e.

$mongo dbname --eval "db.collection1.name.insert({ dataset1}}; db.collection2.name.insert({ dataset2}};"

Perhaps I shouldnt be using the shell for this?

Eliot Horowitz

unread,
Sep 6, 2010, 1:38:00 PM9/6/10
to mongod...@googlegroups.com
That will work, but wouldn't it be easier to go straight?  What language are your tests in?

rick....@gmail.com

unread,
Sep 6, 2010, 1:41:35 PM9/6/10
to mongod...@googlegroups.com
I'm writing everything in Javascript (using nodejs). The BDD framework I'm using is vows (vowsjs.org)

There is an excellent driver available (http://github.com/christkv/node-mongodb-native) which I use in my main application -

I'm just wasnt sure what the preferred approach to populating mongo databases for my BDD specs for to use.

Eliot Horowitz

unread,
Sep 6, 2010, 3:29:58 PM9/6/10
to mongod...@googlegroups.com
I would recommend talking to mongo directly using the node driver. 
Reply all
Reply to author
Forward
0 new messages