Docs Home / mongosh / Configure

Use an Editor for Commands使用命令编辑器

The mongosh console is line oriented. However, you can also use an editor to work with multiline functions. There are two options:猫鼬控制台是面向线条的。但是,您也可以使用编辑器来处理多行函数。有两种选择:

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 albums is 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退出并运行函数。

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. The editor property overrides the EDITOR environment variable.如果还设置了mongosh编辑器属性,mongosh将使用该程序。编辑器属性覆盖editor环境变量。

Set the EDITOR environment variable设置EDITOR环境变量

The environment variable should be set before starting mongosh.应该在启动mongosh之前设置环境变量。

Set an environment variable in bash or zsh:bashzsh中设置环境变量:

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属性

To 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:要设置示例,请执行以下操作:

  1. Copy the example code.复制示例代码。
  2. Enter edit to start an editing session.输入edit以启动编辑会话。
  3. Paste the example code into the editor.将示例代码粘贴到编辑器中。
  4. Exit the editor.退出编辑器。
  5. 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 --wait with Visual Studio.Visual Studio需要一个特殊参数才能作为外部编辑器工作。在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 PATH, open mongosh and run:您还可以使用config.set()设置编辑器。如果Visual Studio在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 EDITOR from your shell.要使更改持久,请从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 to exit and run your function<ctrl>+d退出并运行功能
  • Press <ctrl> + c to 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关闭后可用。