MongoDB – How to Disable MongoDB Shell Logs

mongodb

Whenever we connect to mongo thru shell, we get some logs. I just want the query result not the other logs except the failure message. Is there any way to configure that?

MongoDB shell version v3.6.11
connecting to: mongodb://localhost:27070/test?authMechanism=MONGODB-X509
2020-04-10T21:40:59.305+0000 I NETWORK [thread1] Starting new replica set monitor for 
2020-04-10T21:40:59.516+0000 W NETWORK [ReplicaSetMonitor-TaskExecutor-0] SSL peer certificate validation failed: self signed certificate in certificate chain
2020-04-10T21:40:59.519+0000 W NETWORK [thread1] SSL peer certificate validation failed: self signed certificate in certificate chain
2020-04-10T21:40:59.586+0000 I NETWORK [thread1] Successfully connected to localhost:27070 (1 connections now open to localhost:27070  with a 5 second timeout)
2020-04-10T21:40:59.586+0000 I NETWORK [ReplicaSetMonitor-TaskExecutor-0] Successfully connected to localhost:27070  (1 connections now open to localhost:27070  with a 5 second timeout)
2020-04-10T21:40:59.653+0000 I NETWORK [thread1] changing hosts to ....
2020-04-10T21:40:59.865+0000 W NETWORK [ReplicaSetMonitor-TaskExecutor-0] SSL peer certificate validation failed: self signed certificate in certificate chain
2020-04-10T21:40:59.868+0000 W NETWORK [thread1] SSL peer certificate validation failed: self signed certificate in certificate chain
2020-04-10T21:40:59.932+0000 I NETWORK [ReplicaSetMonitor-TaskExecutor-0] Successfully connected to localhost:27070  (1 connections now open to localhost:27070  with a 5 second timeout)
2020-04-10T21:41:00.007+0000 W NETWORK [ReplicaSetMonitor-TaskExecutor-0] Failed to connect to localhost:27070, in(checking socket for error after poll), reason: Connection refused
2020-04-10T21:41:00.019+0000 W NETWORK [ReplicaSetMonitor-TaskExecutor-0] SSL peer certificate validation failed: self signed certificate in certificate chain
2020-04-10T21:41:00.021+0000 I NETWORK [ReplicaSetMonitor-TaskExecutor-0] Successfully connected to localhost:27070  (1 connections now open to localhost:27070  with a 5 second timeout)
2020-04-10T21:41:00.032+0000 W NETWORK [ReplicaSetMonitor-TaskExecutor-0] SSL peer certificate validation failed: self signed certificate in certificate chain
2020-04-10T21:41:00.032+0000 I NETWORK [ReplicaSetMonitor-TaskExecutor-0] Successfully connected to localhost:27070   (1 connections now open to localhost:27070  with a 5 second timeout)
Implicit session: session { "id" : UUID("0a2c342e-6afe-4d1e-zz98-49110d4a1767") }
MongoDB server version: 3.6.8
2020-04-10T21:41:00.471+0000 E QUERY [thread1] Error: don't know how to show [collections()] :
shellHelper.show@src/mongo/shell/utils.js:997:11
shellHelper@src/mongo/shell/utils.js:750:15
@(shellhelp2):1:1
bye

Best Answer

You can use the --quiet option to suppress output including the mongo shell version and startup warnings during the connection process.

Those startup warnings are usually issues an administrator would want to be aware of, but you may prefer to suppress them for a development environment or for running scripts. For a production system you would be best addressing the reported issues to remove future startup warnings.

For example, connecting to a 3.6 deployment with a 4.4 shell:

$ mongo 
MongoDB shell version v4.4.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f9219e8d-01ff-46e6-9e9d-b4de68207bdf") }
MongoDB server version: 3.6.17
WARNING: shell and server versions do not match
---
The server generated these startup warnings when booting:
2021-02-04T21:57:55.555+1100 I CONTROL  [initandlisten]
2021-02-04T21:57:55.555+1100 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2021-02-04T21:57:55.555+1100 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2021-02-04T21:57:55.555+1100 I CONTROL  [initandlisten]
2021-02-04T21:57:55.555+1100 I CONTROL  [initandlisten]
2021-02-04T21:57:55.555+1100 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
---

Adding --quiet and connecting to the same MongoDB deployment:

$ mongo --quiet
> 

The warnings being suppressed in this example indicate:

  • My mongo shell version (v4.4.3) is much newer than my server version (v3.6.17). Mixing these versions may have unexpected outcomes, particularly given so many intervening MongoDB releases. For a local installation I normally expect these versions to be identical, and for production usage I would want the major versions to be matching (i.e. both v4.4.x or v3.6.x).
  • Access control is not enabled on this deployment, so anyone can connect with full access to read & write data. This is a serious issue for a production deployment, but I would follow the MongoDB Security Checklist for any deployment where you are concerned about securing your data.
  • Resource limits are too low, so this deployment may run into problems with too many connections or open files. I would certainly fix this warning for a production environment, but also address for a development environment to avoid unexpected interruptions.