Navigation

    Retrieve MongoDB Shell Logs

    Beta

    The MongoDB Shell (mongosh) is currently available as a Beta release. The product, its features, and the corresponding documentation may change during the Beta stage.

    MongoDB Shell stores logs for each session in ndjson format using pino.

    You can view or tail the logs for a MongoDB Shell session based on its session ID.

    note

    The MongoDB Shell redacts credentials from the command history and the logs.

    View MongoDB Shell Logs

    1

    Find your Session ID.

    example

    MongoDB Shell displays the session ID each time you open the shell.

    $ mongosh
    
    Current sessionID: c2961dbd6b73b052671d9df0
    Connecting to: mongodb://127.0.0.1:27017
    Using MongoDB: 4.2.8
    Using Mongosh Beta: 0.0.6
    2

    View the log for the session.

    MongoDB Shell saves the log for each session to your user's .mongodb/mongosh directory:

    ~/.mongodb/mongosh/<sessionId>_log

    Run the following command to view the log for a session:

    cat ~/.mongodb/mongosh/<sessionId>_log

    Run the following command to tail the log for a session:

    tail -f ~/.mongodb/mongosh/<sessionId>_log

    tip

    You can pipe the output to an installed ndjson pretty printer, such as pino-colada or pino-pretty to improve readability:

    tail -f ~/.mongodb/mongosh/<sessionId>_log | pino-colada
    %UserProfile%/.mongodb/mongosh/<sessionId>_log

    Run the following PowerShell command to view the log for a session:

    Get-Content %UserProfile%/.mongodb/mongosh/<sessionId>_log

    Run the following PowerShell command to tail the log for a session:

    Get-Content %UserProfile%/.mongodb/mongosh/<sessionId>_log -Wait

    tip

    You can pipe the output to an installed ndjson pretty printer, such as pino-colada or pino-pretty to improve readability:

    Get-Content %UserProfile%/.mongodb/mongosh/<sessionId>_log -Wait | pino-colada

    View MongoDB Shell Command History

    MongoDB Shell saves a history of all commands you've run across sessions. When a new command is issued, it is added to the beginning of the history file.

    Open the following file in a text editor to view the MongoDB Shell command history:

    Operating SystemPath to History File
    macOS and Linux~/.mongodb/mongosh/.mongosh_repl_history
    Windows%UserProfile%/.mongodb/mongosh/.mongosh_repl_history