2

I'm using Ubuntu 14.4 and MongoDB 2.6.6.

I'm writing a bash script and I'm using the following command:

mongo cc --eval "printjson(db.cc_data.count())"

to get the count of the collection cc_data. I would like to save the count returned in a variable, but I couldn't.

2 Answers 2

5

You can assign the output of the shell to a variable. You just need to ensure you suppress the output from the mongo shell during the connection process using --quiet.

let count=`mongo cc --eval "printjson(db.cc_data.count());" --quiet`

You can test the value by running:

echo $count
Sign up to request clarification or add additional context in comments.

Comments

2

In bash you can use the following syntax

var=$( mongo cc --eval "printjson(db.cc_data.count())" --quiet )

Comments

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.