The 猫鼬控制台是面向线条的。但是,您也可以使用编辑器来处理多行函数。有两种选择:mongosh console is line oriented. 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. You can configure an external editor in the shell that runs mongosh or from within mongosh.mongosh-edit命令与外部编辑器配合使用。您可以在运行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.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().edit语句,请使用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> to run the statement or <ctrl> + c to cancel it.<enter>运行语句,或按<ctrl>+c取消语句。
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 to exit and run your function.<ctrl>+d退出并运行函数。
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. The editor property overrides the EDITOR environment variable.mongosh编辑器属性,mongosh将使用该程序。编辑器属性覆盖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编辑器将打开。
Note
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. The update does not persist when mongosh exits.mongosh进行更新。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编辑器将打开。
Note
mongosh will attempt to use whatever program is configured. A program like less will work. Other programs, such as grep, may crash or have unexpected results.mongosh将尝试使用配置的任何程序。像less这样的程序可以工作。其他程序,如grep,可能会崩溃或产生意外结果。
Editing a Command编辑命令
Use 使用edit to start an editing session. If the editor was already used in the current console session, the editor opens the last edit.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. Use Visual Studio需要一个特殊参数才能作为外部编辑器工作。在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(). If Visual Studio is in your 您还可以使用config.set()设置编辑器。如果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. From mongosh, run:EDITOR环境,也可以取消设置。来自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> + dto exit and run your function<ctrl>+d退出并运行功能Press按<ctrl> + cto exit without running your function<ctrl>+c退出而不运行函数
Objects which are declared using 使用.editor, like albums in this example, are added to the global scope. They are available after .editor closes..editor声明的对象(如本例中的albums(相册))将添加到全局范围。它们在.editor关闭后可用。