7

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?

1 Answer 1

12

There are several options for loading JavaScript into the mongo shell:

1) Save the JavaScript to .mongorc.js in your home directory.

2) Save the JavaScript to the global mongorc.js file (MongoDB 2.6+) which is evaluated before the .mongorc.js file in your home directory.

3) Use load('filename.js') from within the shell (or within one of the mongorc.js files).

4) Store your JavaScript on the server using the system.js collection and db.loadServerScripts().

For common JavaScript functions, the first option is the most typical approach.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.