IntelliShell
Posted on: 30/04/2018 (last updated: 09/03/2022) by Kathryn Vargas- tl;dr
IntelliShell is the built-in mongo shell in Studio 3T that offers live error highlighting and smart auto-completion of JavaScript standard library functions, shell-specific types and methods, operators, collection names, field names, and shell helper commands.IntelliShell是Studio 3T中的内置mongo shell,提供实时错误高亮显示和智能自动完成JavaScript标准库函数、特定于shell的类型和方法、运算符、集合名称、字段名称和shell helper命令。Try it today.Shortcuts快捷键Open IntelliShell打开IntelliShell – Ctrl + L (⌘ + L)
Trigger auto-completion触发自动完成 – Ctrl + Space (^ + Space)
Find查找 – Ctrl + F (⌘ + F)
Replace替换 – Ctrl + H (Shift + ⌘ + F)
Comment with line comment用行注释注释 – Ctrl + / (⌘ + /)
Open script打开脚本 – Ctrl + O (⌘ + O)
Save script保存脚本 – Ctrl + S (⌘ + S)
Save script as脚本另存为 – Shift + Ctrl + S (Shift + ⌘ + S)
Execute entire script执行整个脚本 – F5
Execute statement at cursor执行游标处的语句 – F6
Execute selection执行选择 – F9
Prerequisites先决条件
Download Studio 3T and choose the right version for your particular platform and bit size.下载Studio 3T,并根据您的特定平台和位大小选择合适的版本。
IntelliShell requires the mongo shell executable, one of the mongo client tools bundled when downloading MongoDB.IntelliShell需要mongo shell可执行文件,这是下载MongoDB时捆绑的mongo客户端工具之一。
No additional configuration is needed for Windows, macOS, or Linux installations. Windows、macOS或Linux安装不需要额外配置。The mongo shell executable is bundled with Studio 3T on these systems.在这些系统上,mongo shell可执行文件与Studio 3T捆绑在一起。
If you want to use another version of MongoDB, here’s how you can point IntelliShell to use a different mongo.exe file.如果您想使用另一个版本的MongoDB,以下是如何让IntelliShell使用不同的mongo.exe文件。
►https://player.vimeo.com/video/681928818
Custom 自定义mongo shell
If you choose to use your own installation of the mongo shell, you can configure this in Studio 3T.如果您选择使用自己安装的mongo shell,可以在Studio 3T中进行配置。
Once the mongo client tools are installed:安装mongo客户端工具后:
Go to Studio 3T > Preferences.进入“Studio 3T”>“首选项”。Click on the IntelliShell tab.单击“IntelliShell”选项卡。Insert the path of where the mongo shell executable is located.插入mongo shell可执行文件所在的路径。Click OK.点击“确定”。
IntelliShell is just one of four ways to query MongoDB in Studio 3T.IntelliShell只是Studio 3T中查询MongoDB的四种方法之一。
Launch IntelliShell启动IntelliShell
Choose a target server, database, or collection in the Connection Tree, then open IntelliShell through three ways:在连接树中选择目标服务器、数据库或集合,然后通过三种方式打开IntelliShell:

Button按钮 –Click on the IntelliShell button in the global toolbar单击全局工具栏中的“IntelliShell”按钮Right-click右击 –Right-click and choose Open IntelliShell右键单击并选择“打开IntelliShell”Shortcut快捷键 –Press Ctrl + L (⌘+ L)按Ctrl+L(⌘+L)
When you open IntelliShell on an existing collection, IntelliShell will open with the default query for that collection. 在现有集合上打开IntelliShell时,IntelliShell将以该集合的默认查询打开。You can use Studio 3T’s Query Manager to set a default query.可以使用Studio 3T的查询管理器设置默认查询。

IntelliShell has two main sections: the top Editor section where you can write queries, commands, and scripts, and the bottom Result tab section where you can view the shell output.IntelliShell有两个主要部分:顶部的“编辑器”部分,可以编写查询、命令和脚本;底部的“结果”选项卡部分,可以查看shell输出。
MongoDB query auto-completionMongoDB查询自动完成
IntelliShell detects and auto-suggests JavaScript standard library functions, shell-specific types and methods, operators, collection names, field names, and shell helper commands as you type.IntelliShell会在键入时检测并自动建议JavaScript标准库函数、特定于shell的类型和方法、运算符、集合名称、字段名称和shell助手命令。

You can trigger query auto-completion in three ways:可以通过三种方式触发查询自动完成:
Button按钮 –Click on the database icon, next to Enable Query Assist单击数据库图标,然后启用“查询辅助”Right-click右击 –Right-click anywhere in the Editor and choose Open Auto-Completion在编辑器中的任意位置单击鼠标右键,然后选择“打开自动完成”Shortcut快捷键 –Press Ctrl + Space (^ + Space)按Ctrl+Space(^+Space)
Live error highlighting实时错误突出显示
IntelliShell highlights syntax errors as you type.IntelliShell会在键入时突出显示语法错误。
These errors are also noted in the left-hand sidebar as red crosses that you can hover over, and in the right-hand script overview ruler as clickable red markers that take you straight to the error line.这些错误在左侧的侧边栏中也会显示为红色的十字,您可以将其悬停在上面,在右侧的脚本概览标尺中,这些错误会显示为可单击的红色标记,直接指向错误行。

Run queries via the mongo shell (Disable Query Assist)通过mongo shell运行查询(禁用查询辅助)
You can execute queries and scripts entirely, at cursor, or just a selection.您可以在游标处完全执行查询和脚本,也可以只执行一个选择。

To run an entire script:要运行整个脚本,请执行以下操作:
Button按钮 –Click on the Run (play) button in the toolbar单击工具栏中的“运行”(播放)按钮Right-click右击 –Right-click anywhere in the Editor and choose Execute Entire Script在编辑器中的任意位置单击鼠标右键,然后选择“执行整个脚本”Shortcut快捷键 –Press F5按F5
To run a script at cursor, place the cursor at any top-level statement and:要在游标上运行脚本,请将游标放在任何顶级语句上,然后执行以下操作:
Button按钮 –Click on the second Run (play) button in the toolbar单击工具栏中的第二次运行(播放)按钮Right-click右击 –Right-click anywhere in the Editor and choose Execute Statement at Cursor在编辑器中的任意位置单击鼠标右键,然后选择“在游标处执行语句”Shortcut快捷键 –Press F6按F6
To run a selection, highlight the part of the query or script to be executed and:要运行选择,请突出显示要执行的查询或脚本的部分,然后:
Button按钮 –Click on the third Run (play) button点击第三个“运行”(播放)按钮Right-click右击 –Right-click anywhere on the selection and choose Execute Selection在所选内容的任意位置单击鼠标右键,然后选择“执行所选内容”Shortcut快捷键 –Press F9按F9
The full list of MongoDB shell operators can be found here, which is also directly accessible within Studio 3T through the Shell Methods Reference link in the toolbar.MongoDB shell运算符的完整列表可以在这里找到,也可以通过工具栏中的“Shell方法参考”链接在Studio 3T中直接访问。
In the Result tab section, you’ll always find the Raw Shell Output tab which displays the output of all shell-run commands.在“结果”选项卡部分,您总能找到“原始Shell输出”选项卡,其中显示所有Shell运行命令的输出。

Should you choose to disable Query Assist in the toolbar, you enable what was previously the Raw Shell Mode in older IntelliShell versions. 如果您选择在工具栏中禁用“查询辅助”功能,则可以在较旧的IntelliShell版本中启用以前的原始Shell模式。Results will appear as text output in the mongo shell.结果将在mongo shell中显示为文本输出。
Whenever possible, Studio 3T will parse documents from the Raw Shell Output and display them in a “prettier” Shell Output Documents tab. 只要有可能,Studio 3T将解析原始Shell输出中的文档,并将它们显示在“更漂亮”的Shell输出文档选项卡中。You can view results through the three standard views (Table, Tree, and JSON), but they will not be editable.您可以通过三个标准视图(表格、树形和JSON)查看结果,但它们不可编辑。

You can run find and aggregation queries, this way but they will no longer open editable result tabs.您可以通过这种方式运行查找和聚合查询,但它们将不再打开可编辑的结果选项卡。
Instead, they will appear along with other shell commands and scripts in the Raw Shell Output tab as you would expect in a real terminal or shell, which means you won’t be able to edit or page through the results. 相反,它们将与其他shell命令和脚本一起出现在“原始shell输出”选项卡中,就像您在实际终端或shell中所期望的那样,这意味着您将无法编辑或翻阅结果。Query Code and Visual Explain will not be available.查询代码和可视化解释将不可用。
Run queries via Studio 3T (Enable Query Assist)通过Studio 3T运行查询(启用查询辅助)
Query Assist is enabled by default for standalone, top-level find and aggregation queries. 对于独立的顶级查找和聚合查询,默认情况下会启用查询辅助功能。This means they will be run through Studio 3T instead of the mongo shell.这意味着它们将通过Studio 3T而不是mongo shell运行。
Query Assist adds functionalities that are not available for shell-run queries, like the ability to:查询辅助功能添加了shell运行查询无法使用的功能,例如:
1. Run find and aggregation queries that open their own powerful result tabs. 运行“查找”和“聚合”查询,打开它们自己强大的结果选项卡。Find queries run using Query Assist allow in-place editing.使用“查询辅助”运行“查找查询”并允许就地编辑。
Assisted queries are clearly labeled in the left-hand sidebar as wand icons, and in the right-hand script overview rulers as clickable yellow markers that take you straight to the query.辅助查询在左侧边栏中明确标记为魔杖图标,在右侧脚本概览标尺中明确标记为可点击的黄色标记,直接指向查询。

Each assisted query will open a separate, editable result tab, whose name will display its corresponding line in the Editor.每个辅助查询将打开一个单独的、可编辑的结果选项卡,其名称将在编辑器中显示相应的行。
2. Edit results within the tabs, as you normally would in other parts of Studio 3T through the three standard views: Table, Tree, and JSON.在选项卡中编辑结果,就像您通常在Studio 3T的其他部分中通过三个标准视图编辑结果一样:表格、树形和JSON。

3. Use Query Code to convert your MongoDB query to JavaScript (Node.js), Java (2.x and 3.x driver API), Python, C#, PHP, and Ruby.使用“查询代码”将MongoDB查询转换为JavaScript(Node.js)、Java(2.x和3.x驱动程序API)、Python、C#、PHP和Ruby。
Click on the Query Code button on any result tab.单击任意结果选项卡上的查询代码按钮。

A Query Code tab will open. 将打开“查询代码”选项卡。Choose your target language from the dropdown menu.从下拉菜单中选择目标语言。

Related reading: 相关阅读:Query Code – Generate code from MongoDB queries instantly查询代码-立即从MongoDB查询生成代码
4. Use Visual Explain to help optimize performance by seeing how MongoDB executed your query.通过查看MongoDB如何执行查询,使用“图形化解释”帮助优化性能。
Click on the Explain Query button on any result tab. 单击任意结果选项卡上的解释查询按钮。This will open a new Explain tab that shows which steps MongoDB took to run the query.这将打开一个新的解释选项卡,显示MongoDB运行查询所采取的步骤。

Related reading:相关阅读: Visual Explain – MongoDB explain(), visualized可视化解释 - MongoDB Explain()
,可视化
Query Assist only assists find and aggregation queries – all other scripts and commands will always be run in the shell. 查询辅助功能仅帮助查找和聚合查询——所有其他脚本和命令将始终在shell中运行。Their results are viewable in the the Raw Shell Output tab.其结果可在“原始外壳输出”选项卡中查看。
Pin only the query results you want to keep仅固定要保留的查询结果
We’ve done away with the Retain option in the old IntelliShell, which as users have told us, led to undistinguishable tabs and general tab clutter.我们已经废除了旧IntelliShell中的Retain选项,正如用户告诉那样,它会导致无法区分的选项卡和一般的选项卡混乱。

Now you can keep things tidy and choose to keep only the query results you need, by clicking on the Pin result button in any result tab.现在,您可以通过单击任何结果选项卡中的“固定结果”按钮来保持整洁,并选择只保留所需的查询结果。

All other unpinned result tabs will be cleared with the next execution, be it the entire script or a portion of it.所有其他未固定的结果选项卡将在下一次执行时清除,无论是整个脚本还是部分脚本。
Format and indent code格式和缩进代码
There are shortcuts in the IntelliShell toolbar to quickly format and indent code:IntelliShell工具栏中有快捷方式可以快速格式化和缩进代码:

To format code:要格式化代码:
Button按钮 –Click on the Format icon, next to Enable Query Assist单击“启用查询辅助”旁边的“格式”图标Right-click右击 –Right-click anywhere in the Editor and choose Format在编辑器中的任意位置单击鼠标右键,然后选择“格式”Shortcut快捷键 –Press按Ctrl + Alt + L (or或 ⌥ + ⌘ + L on Mac)
To indent code:要缩进代码:
Button按钮 –Place a cursor in the target query/query selection and click on the Indent icon将游标放在目标查询/查询选择中,然后单击缩进图标Shortcut快捷键 –Press Tab按Tab
To unindent code:要取消缩进代码:
Button按钮 –Place a cursor in the target query/query selection and click on the Unindent icon将游标放在目标查询/查询选择中,然后单击“取消缩进”图标Shortcut快捷键 –Press按 Shift + Tab
You can define your Editor tab width preferences under Studio 3T > Preferences > General. 您可以在“Studio 3T”>“首选项”>“常规”下定义编辑器选项卡宽度首选项。The default editor tab width setting is 4 spaces.默认的编辑器选项卡宽度设置为4个空格。

Insert and remove comments插入和删除注释
It is also possible to comment and uncomment with a line comment.还可以使用行注释进行注释和取消注释。
Place the cursor on the line to be made into a comment and:将游标放在要注释的行上,然后:
Right-click右击 –Right-click anywhere in the Editor and choose Comment with Line Comment.在编辑器中的任意位置单击鼠标右键,然后选择“带行注释的注释”。Repeat to remove the comment.重复以上步骤以删除评论。Shortcut快捷键 –Press Ctrl + / (⌘+ /).按Ctrl+/(⌘+/)。Repeat to remove the comment.重复以上步骤以删除评论。
Open and save queries打开并保存查询
To save your MongoDB query as a JavaScript file:要将MongoDB查询保存为JavaScript文件,请执行以下操作:
Button按钮 –Click on the Save icon.点击保存图标。Alternatively, click on the arrow to find the Save As function.或者,单击箭头以查找“另存为”功能。Right-click右击 –Right-click anywhere in the Editor and choose Save Script (As)在编辑器中的任意位置单击鼠标右键,然后选择“将脚本另存为”Shortcut快捷键s –Save Script保存脚本 – Ctrl + S (⌘+ S), Save Script As,脚本另存为 – Shift + Ctrl + S (Shift + ⌘+ S)
To open saved queries:要打开保存的查询,请执行以下操作:
Button按钮 –Click on the Open icon点击“打开”图标Right-click右击 –Right-click anywhere in the Editor and choose Open Script在编辑器中的任意位置单击鼠标右键,然后选择“打开脚本”Shortcut快捷键 –Press按 Ctrl + O (⌘+ O)
Use IntelliShell and Visual Query Builder side by side并排使用IntelliShell和“图形化查询生成器”
Tired of manually typing everything? 厌倦了手工打字吗?You can use Visual Query Builder – our drag-and-drop MongoDB query builder – alongside IntelliShell.您可以在IntelliShell旁边使用“图形化查询生成器”(拖放MongoDB查询生成器)。
Simply click on Show Visual Query Builder in the toolbar.只需单击工具栏中的“显示图形化查询生成器”。

We’ve tightened this integration so that edits made in IntelliShell are reflected in Visual Query Builder in real-time, and vice versa. 我们加强了这种集成,以便在IntelliShell中进行的编辑能够实时反映在“图形化查询生成器”中,反之亦然。This way, you can skip the command line writing and build queries visually – or combine both ways to work even faster.通过这种方式,您可以跳过命令行编写,直观地生成查询,或者将这两种方法结合起来,以更快地工作。
Want to extend your use of the mongo shell? Read on:想扩大mongo shell的使用范围吗?请继续阅读:
Discover the Aggregation Editor and break down the MongoDB aggregation pipeline into stages as you build your queries发现“聚合编辑器”,并在构建查询时将MongoDB聚合管道分解为多个阶段Learn how to create a view in MongoDB (or read our practical introduction to MongoDB views)了解如何在MongoDB中创建视图(或阅读对MongoDB视图的实用介绍)