Question

A beginner to mongoDB. went through this website to know how to create a javascript file . .mongorc.js and

<code>
function insertData(dbName, colName, num)
{
var col = db.getSiblingDB(dbName).getCollection(colName);
for (i = 0; i < num; i++) 
{
col.insert({x:i});
}
print(col.count());
}
<code>

and the link is here function insertData ... the tutorial adviced to store the function in a javascript file called .mongorc.js . I dont know how to create it and use it and where to store it . need help .

Was it helpful?

Solution

.mongorc.js

The .mongorc.js file is used if you want to have some JavaScript loaded every time you start the mongo shell:

  • On Linux, Unix, and OS X mongo looks for this file in $HOME/.mongorc.js.

  • On Windows, mongo.exe looks for this file in %HOME%.mongorc.js or %HOMEDRIVE%%HOMEPATH%.mongorc.js.

Typically you only want to store re-usable functions in your .mongorc.js.

Other ways to load JavaScript into a mongo shell session include:

  • using the load() command in a mongo shell: load("/path/to/something.js").

  • passing a JavaScript filename as a command line parameter when starting the mongo shell: mongo /path/to/something.js

  • pasting JavaScript directly into the mongo shell (e.g. copy & paste from a tutorial)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top