I’d like to be able to import a library of common tools that I use to make working with MongoDB easier. However, I haven’t discovered an easy to to import external scripts into the Mongo CLI. Ideally, this would work similar to Node.js require.
How can I get require working in Mongo CLI by default?
Or, is there some other way to solve external library dependencies?
There are several options for loading JavaScript into the
mongoshell:1) Save the JavaScript to
.mongorc.jsin your home directory.2) Save the JavaScript to the global
mongorc.jsfile (MongoDB 2.6+) which is evaluated before the.mongorc.jsfile in your home directory.3) Use
load('filename.js')from within the shell (or within one of themongorc.jsfiles).4) Store your JavaScript on the server using the
system.jscollection anddb.loadServerScripts().For common JavaScript functions, the first option is the most typical approach.