Michael Millar
Michael Millar

Reputation: 1604

mongo find() producing no output with eval

I'm trying to do a 'find all' of resources within a mongodb collection.

I can get the count ok:

mongo ip:port/database1 -u correctusername -p correctpassword 
--authenticationDatabase admin --eval "db.getCollection('collection_123').count()"

But when I try to tweak it,

mongo ip:port/database1 -u correctusername -p correctpassword 
--authenticationDatabase admin --eval "db.getCollection('collection_123').find()"

It produces

MongoDB shell version: 2.4.0
connecting to: ip:port/database1
DBQuery: database1.collection_123 -> { }

I also tried find({})

There doesn't seem to be authentication errors. The first query works and produces a count. I can also

mongo ip:port/database1 -u correctusername -p correctpassword 
--authenticationDatabase admin

get into the mongo shell and

db.getCollection('collection_123').find()

And see the correct json output.

Upvotes: 3

Views: 1529

Answers (1)

Michael Millar
Michael Millar

Reputation: 1604

The only way I have found is to

mongo  ip:port/database1 -u correctusername -p correctpassword
--authenticationDatabase admin --eval "db.collection.find().forEach(printjson)"

but the output is not the same as in the shell, as further commands will not work, like sort or limit.

Update

A better solution is

mongo  ip:port/database1 -u correctusername -p correctpassword
--authenticationDatabase admin --eval "printjson(db.collection.find().toArray())"

Upvotes: 2

Related Questions