2

I would like to read data from remote mongo database server. I tried by below ways.

ssh username@hostname "mongo databaseName --eval 'db.collection.find({\"params1\":\"2\",\"params2\":\"2\"})'"

or

ssh username@hostname "mongo databaseName --eval 'printjson(db.collection.find({\"params1\":\"2\", \"params2\":\"2\"}))'"

I used both ways, but I can't get the correct result output from database server. How should I do ?

1 Answer 1

6

The following format should work, note that I specifically included the full path of the mongo executable on the remote database server. If this command does not work, you should ensure that SSH is running on the server and check to see if there is any authentication settings on your database.

ssh username@hostname "/usr/local/mongodb/bin/mongo --norc --eval 'db.collection.find().forEach(printjson)'"
Sign up to request clarification or add additional context in comments.

2 Comments

Connection is OK but I don't get the result output. MongoDB shell version: 2.2.0 connecting to: test DBQuery: test.groups -> undefined
If you want to capture the output using a script (I'll use bash to illustrate) you will need to use a variable and you will also need to redirect the standard error (stderr) to the standard output (stdout) to get the result you want: result=$(ssh username@hostname "/usr/local/mongodb/bin/mongo --norc --eval 'db.collection.find().forEach(printjson)'" 2>&1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.