Use an Editor for Commands使用命令编辑器
On this page本页内容
The mongosh console is line oriented. mongosh控制台是面向线条的。However, you can also use an editor to work with multiline functions. 但是,您也可以使用编辑器来处理多行函数。There are two options:有两种选择:
Use the将editcommand with an external editor.edit命令与外部编辑器一起使用。Use the使用.editorcommand, a built-in editor..editor命令,一个内置的编辑器。
Using an External Editor使用外部编辑器
The mongosh edit command works with an external editor. mongosh edit命令可与外部编辑器配合使用。You can configure an external editor in the shell that runs 您可以在运行mongosh or from within mongosh.mongosh的shell中或从mongosh内部配置外部编辑器。
If editors are configured in both locations, the editor configured within 如果在这两个位置都配置了编辑器,则mongosh will take precedence.mongosh中配置的编辑器将优先。
To set an editor within 要在mongosh, use the config.set() command.mongosh中设置编辑器,请使用config.set()命令。
config.set( "editor", "vi" )
See setting the external editor for more examples.有关更多示例,请参阅设置外部编辑器。
You can use 您可以通过以下三种方式使用edit in three ways:edit:
Start a New Editing Session启动新的编辑会话
Enter 输入edit by itself to start a new editing session.edit自行启动新的编辑会话。
edit
If you start an editing session without any arguments, the editor opens with the last edit loaded. 如果在没有任何参数的情况下启动编辑会话,则编辑器将以加载的最后一个编辑打开。See the example, Editing a Command.请参见示例“编辑命令”。
Edit a Variable编辑变量
If an argument exists in the console namespace, you can use 如果控制台命名空间中存在参数,则可以使用edit to update it.edit对其进行更新。
var albums = [ ];
edit albums
The variable变量albumsis set in the first line.albums设置在第一行。The second line opens the external editor to edit the value of第二行打开外部编辑器以编辑albums.albums的值。
Edit a Statement编辑语句
To edit a statement in the external editor, invoke 要在外部编辑器中编辑语句,请使用edit with a statement such as db.collection.insertMany().db.collection.insertMany()等语句调用edit。
edit db.digits.insertMany( [] )
After editing 编辑db.music.insertMany( [] ) and exiting the external editor, the mongosh console might look like this:db.music.insertMany( [] )并退出外部编辑器后,mongosh控制台可能如下所示:
prompt> db.digits.insertMany([{ "zero": 0 }, { "one": 1 }, { "two": 2 }])
When you exit the external editor, the statement is copied to the console input line, ready to run. It does not run automatically. 当您退出外部编辑器时,语句被复制到控制台输入行,准备运行。它不会自动运行。Press 按<enter>运行该语句,或按<ctrl>+c取消该语句。<enter> to run the statement or <ctrl> + c to cancel it.
Using the Built-in Editor使用内置编辑器
The .editor command provides basic multiline editing capabilities..editor命令提供了基本的多行编辑功能。
The editor does not save code. 编辑器不保存代码。When you close the built-in editor, your edits are loaded into the global scope. 当您关闭内置编辑器时,您的编辑将加载到全局范围中。If your edit calls any functions or commands, they will run when you close the editor.如果您的编辑调用了任何函数或命令,它们将在关闭编辑器时运行。
To start the built-in editor:要启动内置编辑器:
.editor
Enter 输入<ctrl>+d退出并运行您的函数。<ctrl> + d to exit and run your function.
See Using the Built-In Editor.请参见使用内置编辑器。
Examples示例
Set the External Editor设置外部编辑器
If the 如果在运行EDITOR environment variable is set in the shell running mongosh, the edit command will use that editor.mongosh的shell中设置了EDITOR环境变量,则edit命令将使用该编辑器。
If the 如果还设置了mongosh editor property is also set, mongosh will use that program instead. mongosh editor属性,mongosh将使用该程序。The editor property overrides the EDITOR environment variable.editor属性将覆盖EDITOR环境变量。
Set the EDITOR environment variable设置EDITOR环境变量
EDITOR environment variableThe environment variable should be set before starting 应该在启动mongosh.mongosh之前设置环境变量。
Set an environment variable in 在bash or zsh:bash或zsh中设置环境变量:
export EDITOR=vi
The 当您在vi editor will open when you run edit in the mongosh console.mongosh控制台中运行edit时,vi编辑器将打开。
You can also set environment variables from within 您还可以使用mongosh using process.env.<VARIABLE>.process.env.<VARIABLE>从mongosh中设置环境变量。
Set the EDITOR environment variable from 从mongosh:mongosh设置EDITOR环境变量:
process.env.EDITOR = 'nano'
The environment variable is only updated for the current 环境变量仅针对当前mongosh. mongosh进行更新。The update does not persist when 当mongosh exits.mongosh退出时,更新不会持续。
Set the editor Property设置editor属性
editor PropertyTo set 要从nano as the editor from within mongosh, use the config.set() command.mongosh中将nano设置为编辑器,请使用config.set()命令。
config.set( "editor", "nano" )
The 当您在nano editor will open when you run edit in the mongosh console.mongosh控制台中运行edit时,nano编辑器将打开。
mongosh will attempt to use whatever program is configured. 将尝试使用配置的任何程序。A program like 像less will work. less这样的程序会起作用。Other programs, such as 其他程序,如grep, may crash or have unexpected results.grep,可能会崩溃或出现意外结果。
Editing a Command编辑命令
Use 使用edit to start an editing session. edit启动编辑会话。If the editor was already used in the current console session, the editor opens the last edit.如果编辑器已在当前控制台会话中使用,则编辑器将打开最后一次编辑。
The following statement has a syntax error. 以下语句存在语法错误。The highlighted line is missing a comma:突出显示的行缺少逗号:
// WARNING: This code contains an error
db.users.insertMany( [
{ "name": "Joey", "group": "sales" }
{ "name": "Marie", "group": "sales" },
{ "name": "Elton", "group": "accounting" },
{ "name": "Paola", "group": "marketing" }
] )
To set up the example:设置示例:
Copy the example code.复制示例代码。Enter输入editto start an editing session.edit以启动编辑会话。Paste the example code into the editor.将示例代码粘贴到编辑器中。Exit the editor.退出编辑器。Press按enter键。enter.
When you exit the editor, it copies the example code to the command line. 当您退出编辑器时,它会将示例代码复制到命令行。mongosh returns an error when the code runs.mongosh在代码运行时返回一个错误。
To reload the example code, enter 若要重新加载示例代码,请输入不带任何参数的edit without any arguments.edit。
// WARNING: This code contains an error
db.users.insertMany([{
"name": "Joey",
"group": "sales"
} {
"name": "Marie",
"group": "sales"
},
{
"name": "Elton",
"group": "accounting"
},
{
"name": "Paola",
"group": "marketing"
}
])
The code is reformatted for easier editing. 代码已重新格式化,以便于编辑。In this case the missing comma in the highlighted line causes the documents to be misaligned.在这种情况下,突出显示的行中缺少逗号会导致文档错位。
Using Visual Studio as an External Editor使用Visual Studio作为外部编辑器
Visual Studio requires a special parameter to work as an external editor. Visual Studio需要一个特殊的参数才能用作外部编辑器。Use 对Visual Studio使用--wait with Visual Studio.--wait。
Set an environment variable:设置环境变量:
export EDITOR="/usr/local/bin/code --wait"
You can also set the editor with config.set(). 您也可以使用config.set()设置编辑器。If Visual Studio is in your 如果Visual Studio在您的PATH, open mongosh and run:PATH中,请打开mongosh并运行:
config.set("editor", "code --wait")
If you use Visual Studio, you can also use the MongoDB VS Code Extension.如果您使用Visual Studio,还可以使用MongoDB VS Code扩展。
Unset the External Editor取消设置外部编辑器
Unset the 在editor variable in mongosh:mongosh中取消设置editor变量:
config.set("editor", null)
If the 如果配置了EDITOR environment is configured, unset it as well. EDITOR环境,也可以取消设置。From 从mongosh, run:mongosh开始,运行:
process.env.EDITOR = ''
If you unset 如果使用EDITOR using process.env the change will not persist after exiting mongosh. process.env取消设置EDITOR,则退出mongosh后更改将不会持续。To make the change persistent, unset 要使更改持久化,请从shell中取消设置EDITOR from your shell.EDITOR。
Using the Built-In Editor使用内置编辑器
Start the editor:启动编辑器:
.editor
mongosh enters editor mode. 进入编辑器模式。Enter your code:输入您的代码:
// Entering editor mode (^D to finish, ^C to cancel)
var albums =
[
{ "artist": "Beatles", "album": "Revolver" },
{ "artist": "The Monkees", "album": "Head"}
]
db.music.insertMany( albums )
To leave the editor,离开编辑器,
Press按<ctrl>+d退出并运行您的功能<ctrl> + dto exit and run your functionPress按<ctrl>+c退出而不运行您的功能<ctrl> + cto exit without running your function
Objects which are declared using 使用.editor, like albums in this example, are added to the global scope. .editor声明的对象(如本例中的albums)被添加到全局范围中。They are available after 它们在.editor closes..editor关闭后可用。