Docs HomeMongoDB Shell

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

The mongosh console is line oriented. mongosh控制台是面向线条的。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. 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 albums is 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> 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. 如果还设置了mongosh editor属性,mongosh将使用该程序。The editor property overrides the EDITOR environment variable.editor属性将覆盖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. 环境变量仅针对当前mongosh进行更新。The update does not persist when mongosh exits.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. 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:设置示例:

  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. Visual Studio需要一个特殊的参数才能用作外部编辑器。Use --wait with 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(). 您也可以使用config.set()设置编辑器。If Visual Studio is in your PATH, open mongosh and run:如果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. 如果配置了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 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. 使用.editor声明的对象(如本例中的albums)被添加到全局范围中。They are available after .editor closes.它们在.editor关闭后可用。