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>
?
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>
?
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:
edit
only edits a single variable or function.edit
detects changes when your editor closes a temporary file; with load()
you can test successive edits by saving in your editor without closing.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.