3

When I try to execute a JavaScript code using --eval, mongo shell generates extra output, --quiet option doesn't seem to work

check_mongodb_availability() {
  local hostname=$1
  local database=$2
  local js=" var found = 0;
             var dbs = db.getMongo().getDBNames();
              for(var i in dbs) {
                if (dbs[i].toString() == '"${database}"') found = 1;
              }
              if (found === 1) print(true); else print(false);
           "

  if [ $(mongo --host $hostname --eval "$js" --quiet) == "true" ]; then
    return 1
  else
    return 0
  fi
}

Above function fails saying 'too many arguments'

It's giving below out put

2019-10-10T10:37:57.262+0000 I NETWORK [js] Starting new replica set monitor for replicaSet/172.31.79.62:27017,172.31.69.171:27017,172.31.73.190:27017 2019-10-10T10:37:57.263+0000 I NETWORK [js] Successfully connected to 172.31.69.171:27017 (1 connections now open to 172.31.69.171:27017 with a 5 second timeout) 2019-10-10T10:37:57.263+0000 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to 172.31.79.62:27017 (1 connections now open to 172.31.79.62:27017 with a 5 second timeout) 2019-10-10T10:37:57.264+0000 I NETWORK [js] changing hosts to replicaSet/172.31.79.62:27017,mongodb-replica-2:27017,mongodb-replica-3:27017 from replicaSet/172.31.69.171:27017,172.31.73.190:27017,172.31.79.62:27017 2019-10-10T10:37:57.265+0000 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to mongodb-replica-2:27017 (1 connections now open to mongodb-replica-2:27017 with a 5 second timeout) 2019-10-10T10:37:57.265+0000 I NETWORK [js] Successfully connected to mongodb-replica-3:27017 (1 connections now open to mongodb-replica-3:27017 with a 5 second timeout) true

I need to print just 'true' or 'false' by avoiding all the extra noise

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Mahela Wickramasekara
  • 603
  • 1
  • 11
  • 22

1 Answers1

0

Unfortunately it appears this is an active bug first started in 2016...

https://jira.mongodb.org/browse/SERVER-27159

Two possible workarounds:

  • Remove the replica set name from the host list. EX: host-1:27017,host-2:27017,host-3:27017 instead of my-replica-set:host-1:27017,host-2:27017,host-3:27017
  • Pipe the output through tail