此版本的文档已存档,不再提供支持。要升级5.0部署,请参阅 MongoDB 6.0升级程序。
注意
此处列出的原生方法适用于旧版 mongo shell 。
要使用mongosh 中的原生方法,请参阅 mongosh中的原生方法。
isInteractive()4.2 版本中的新增功能。
返回一个布尔值,指示当前
mongosh会话是在交互模式下运行还是在脚本模式下运行:true如果处于交互模式false如果处于脚本模式
示例
交互模式
在 Shell 内发出操作
isInteractive()
该方法返回true 。
在其中运行 Javascript 文件 mongosh
创建一个包含以下内容的 JavaScript testExample.js文件:
print("Is the shell in interactive mode? " + isInteractive() );
将mongosh连接到部署。 在mongosh中,加载JavaScript文件(请参阅load() ):
let loadStatus = load("testExample.js"); //You may need to specify the path to the file
该方法在mongosh中返回以下内容:
Is the shell in interactive mode? true
脚本模式
创建一个包含以下内容的 JavaScript testExample.js文件:
print("\n\nIs the shell in interactive mode? " + isInteractive() );
从终端/命令行提示符(即 不在mongosh内),则将 JavaScript文件指定为mongosh以执行该文件,如下所示:
mongosh localhost:27017/test testExample.js
该操作将有关 MongoDB Shell 版本的信息和各种信息打印到终端,后跟:
MongoDB shell version v4.4.0 ... Is the shell in interactive mode? false