3

When running a javascript file using the mongoshell the program prints out some information which is expected. However at the top of the screen the first line says "switched to db foo" resulting from the "use foo" command in the program.

use foo;    // switched to db foo
db.myColl.find(); // { "bar" : "baz" }

The program switches among several databases so each time the "use" statement is invoked it produces a couple of lines of unwanted output.

How can the output of the "switched to db foo" line be suppressed from within the javascript program?

ciso
  • 2,887
  • 6
  • 33
  • 58
  • Why do you want the line removed? How are you connecting to the db? You should be able to specify which collection to use on initial connection. – Paul Apr 07 '18 at 17:11
  • Sorry, my comment was sloppily written which led to confusion. My comment should have said "...specify which db to use..." and a more clear way to word my 2nd question is, "How are you connecting to the MongoDB instance?". My point was that if you specify the db name when connecting to the instance then you don't have to use `use`. See https://docs.mongodb.com/manual/reference/program/mongo/#cmdoption-mongo-arg-db – Paul Apr 07 '18 at 17:26
  • Running the mongo.exe program to enter the shell. The mongo instance is running as a service on system startup. The question is updated as to why the suppression is wanted within the javascript program. – ciso Apr 07 '18 at 17:44
  • Why are you connecting to MongoDB using the mongo shell? Why not just use the MongoDB database driver (http://mongodb.github.io/node-mongodb-native/2.2/tutorials/connect/)? – Luke Woodward Apr 08 '18 at 12:28
  • This is used in other places. However the shell is a quick and convenient way to access the database for an ad-hoc need. The question is how to suppress the output of the "use" statement from a javascript script running via the mongo shell. – ciso Apr 09 '18 at 00:11

1 Answers1

1

If you change the database inside a function, the 'switched to db ...' message is not printed. (at least in Robo 3T shell window)

function findInDB (dbName) {
    db = db.getSiblingDB(dbName);
    db.myColl.find();
}
Troglo
  • 1,497
  • 17
  • 18