3

I am writing a bash script in our linux server about extracting a report from MongoDB. In fact, there are two more replica servers and I should extract the report if the it is master at that time for that server. If not, script will not extract anything.

Is there a way to get that information from shell? Thanks.

1 Answer 1

5

you have to run a command to find out if it the PRIMARY or not.

i have a "js" file which contains this:

`printjson(db.isMaster().ismaster);

then in my bash script i run execute this and act accordingly:

PRIMARY=`/usr/bin/mongo ${SERVERNAME}:${PORT} --quiet     ${SCRIPTDIR}/isMaster.js`;
#
#
if [ "$PRIMARY" != "false" ]; then
# it is the PRIMARY

cheers

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

1 Comment

Thanks I just use it like this: mongo --quiet --eval "d=db.isMaster(); print( d['ismaster'] );"

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.