0

In MongoDB Shell, there is a command edit <variable> to inspect/modify the value by your favourite editor specified by EDITOR.

But how can I create an alias for edit, such as e <variable>?

ruanhao
  • 4,663
  • 6
  • 28
  • 43

1 Answers1

0

The edit command is part of the C++ implementation of the mongo shell (src/mongo/shell/dbshell.cpp#L470 in the MongoDB GitHub repo). Native functions like edit are exposed in the interactive shell interpreter but are not readily available to invoke or override via JavaScript (see: Differences Between Interactive and Scripted mongo).

As at MongoDB 3.4 I'm not aware of any obvious way to alias a native code function unless you're keen to modify the source code and build a custom mongo shell.

However, if you are writing any significant scripts for the mongo shell a much more recommendable approach would be to use the load(...) command instead of edit.

Advantages of load() over edit include:

  • Ability to edit multiple variables and functions in a single file.
    • edit only edits a single variable or function.
  • Detect JavaScript syntax errors before you close your draft
    • edit detects changes when your editor closes a temporary file; with load() you can test successive edits by saving in your editor without closing.
    • If edit encounters any JavaScript syntax errors when a file is closed, you'll lose your draft and the variable in the shell will remain at the original value.
  • Your working files are saved in a non-temporary path so you can commit them to version control.
Stennie
  • 63,885
  • 14
  • 149
  • 175