isInteractive()

  • isInteractive()

New in version 4.2.

Returns a boolean value indicating if the currentmongo shell is running in interactive mode or inscript mode:

  • true if in interactive mode
  • false if in script mode

Examples

Interactive Mode

Issuing Operations inside the Shell

Connect a mongo shell to a deployment. Inside themongo shell, type:

  1. isInteractive()

The method returns true.

Running a Javascript File inside the mongo Shell

Create a JavaScript testExample.js file with the content:

  1. print("Is the shell in interactive mode? " + isInteractive() );

Connect a mongo shell to a deployment. Inside themongo shell, load the javascript file (seeload()):

  1. let loadStatus = load("testExample.js"); //You may need to specify the path to the file

The method returns in the mongo shell:

  1. Is the shell in interactive mode? true

Script Mode

Create a JavaScript testExample.js file with the content:

  1. print("\n\nIs the shell in interactive mode? " + isInteractive() );

From a terminal/command-line prompt (i.e. not inside themongo shell), specify the javascript file to themongo shell to execute the file:

  1. mongo localhost:27017/test testExample.js

The operation prints to the terminal information about the MongoDBshell version and various information followed by:

  1. MongoDB shell version v4.2.0
  2. ...
  3.  
  4.  
  5. Is the shell in interactive mode? false