Run javascript script (.js file) in mongodb including another file inside js - mongodb

I want to write a long script for inserting and updating mongodb data.
Is it possible to call external js file that contains the script?
Is it possible to include another js file from the running js file?

Use Load function
load(filename)
You can directly call any .js file from the mongo shell, and mongo will execute the JavaScript.
Example : mongo localhost:27017/mydb myfile.js
This executes the myfile.js script in mongo shell connecting to mydb database with port 27017 in localhost.
For loading external js you can write
load("/data/db/scripts/myloadjs.js")
Suppose we have two js file myFileOne.js and myFileTwo.js
myFileOne.js
print('From file 1');
load('myFileTwo.js'); // Load other js file .
myFileTwo.js
print('From file 2');
MongoShell
>mongo myFileOne.js
Output
From file 1
From file 2

Another way is to pass the file into mongo in your terminal prompt.
$ mongo < myjstest.js
This will start a mongo session, run the file, then exit.
Not sure about calling a 2nd file from the 1st however. I haven't tried it.

Yes you can.
The default location for script files is data/db
If you put any script there you can call it as
load("myjstest.js") // or
load("/data/db/myjstest.js")

for running mutilple js files
#!/bin/bash
cd /root/migrate/
ls -1 *.js | sed 's/.js$//' | while read name; do
start=`date +%s`
mongo localhost:27017/wbars $name.js;
end=`date +%s`
runtime1=$((end-start))
runtime=$(printf '%dh:%dm:%ds\n' $(($runtime1/3600)) $(($secs%3600/60)) $(($secs%60)))
echo ############# $runtime $name.js completed ###########
echo "$name.js completed"
sync
echo 1 > /proc/sys/vm/drop_caches
echo 2 > /proc/sys/vm/drop_caches
echo 3 > /proc/sys/vm/drop_caches
done

To call external file you can use :
load ("path\file")
Exemple: if your file.js file is on your "Documents" file (on windows OS), you can type:
load ("C:\users\user_name\Documents\file.js")

Related

How to load a file from the same directory as the script, in MongoDB shell

I'm running a bash script that executes a javascript file in the Mongo shell:
outerscript.sh
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
LOGDIR=~/logs
LOGFILE=$LOGDIR/outerscript.log
mongo < $SCRIPTPATH/jsscript.js >> $LOGFILE 2>&1
jsscript.js
load(myVars.js);
MongoDB is v4.0.19
The script may be run locally or on a remote server, and it may be run from the command line or from a cron job, and I want to load in myVars.js which is always in the same folder as outerscript.sh.
I can't find a way to load myVars.js when running outerscript.sh from a cron job.
pwd() returns the user's home directory, not the location of the currently running script, so this doesn't work for the cron job:
load(pwd() + '/myVars.js');
This also doesn't work for the cron job, I'm guessing for the same reason:
load('./myVars.js');
Alternatively, is there another way to pass variables into my JavaScript file, that will still let me log output to the logfile? As far as I can tell, you can't combine --eval with logging output to a file, and I haven't found a way to read environment variables directly from inside the JS file.
Thanks!
You don't need a redirect, try mongo $SCRIPTPATH/jsscript.js >> $LOGFILE 2>&1
See
$ mongo --help
MongoDB shell version v4.4.1
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.168.0.5/foo foo database on 192.168.0.5 machine
192.168.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999
mongodb://192.168.0.5:9999/foo connection string URI can also be used
You can use cat() in your js script.
e.g.
echo '{"path": "some folder"}' > /tmp/foldername.js
Then in your js script:
var txt = cat("/tmp/foldername.js")
var folderObj = JSON.parse(txt)
Then you can use folderObj object in your script.
Why do you not simply use SCRIPTPATH="$(dirname "$0")"?
Wernfried Domscheit answered my question perfectly and I have accepted that answer.
I also worked out how to pass in environment variables as parameters and am posting it here just in case it helps anybody.
outerscript.sh
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
ENVVARS='var mongoAddress='\'"${MONGO_ADDRESS}"\''; var databaseName='\'"${DATABASE_NAME}"\'
mongo --eval "${ENVVARS}" $SCRIPTPATH/jsscript.js >> $LOGFILE 2>&1
jsscript.js
print(mongoAddress);
print(databaseName);

mongodb script file prints result along with log

bank_data collection has 6 records in it. so i am expecting the output should show only 6. but the 6 is printed along with timestamp and other text log.
script.js has following code
use test;
db.bank_data.find({first_name : "logan"}).count();
ran the above file in command line
mongo --quiet script.js > result.txt
Output result.txt file has following out put
2015-05-27T17:12:09.752+0530 I CONTROL Hotfix KB2731284 or later update is installed, no need to zero-out data files
switched to db test
6
I need to print the value alone 6
This should work , eval option is one way to get this done:
mongo test --quiet --eval "printjson(db.bank_data.find({first_name :
"logan"}).count())" > result.txt

bash: mongodb/bin/mongod : No such file or directory

I'm trying to install and run some basic commands on mongodb from this tutorial (on page 4).
So far,
I've downloaded the a 32-bit zip file from the mongodb download page.
I've unzip the file in the following locations home/user1 then renamed it to mongodb
I've create a folder called data then another one inside called db
I've created a file name mongodb.config containing the path of the folder where I want to store my data, which is mongodb/data/db.
But when I run the command to start the server:
user1#user1-VirtualBox: ~$ mongodb/bin/mongod --dbpath ~mongodb/data/db
I'm getting the following error:
bash: mongodb/bin/mongod : No such file or directory
Am I missing something?
Missing a '/', it should be ~/mongodb/data/db, not '~mongodb/data/db'

Printing Mongo query output to a file while in the mongo shell

2 days old with Mongo and I have a SQL background so bear with me. As with mysql, it is very convenient to be in the MySQL command line and output the results of a query to a file on the machine. I am trying to understand how I can do the same with Mongo, while being in the shell
I can easily get the output of a query I want by being outside of the shell and executing the following command:
mongo localhost:27017/dbname --eval "printjson(db.collectionName.findOne())" > sample.json
The above way is fine, but it requires me to exit the mongo shell or open a new terminal tab to execute this command. It would be very convenient if I could simply do this while still being inside the shell.
P.S: the Question is an offshoot of a question I posted on SO
AFAIK, there is no a interactive option for output to file, there is a previous SO question related with this: Printing mongodb shell output to File
However, you can log all the shell session if you invoked the shell with tee command:
$ mongo | tee file.txt
MongoDB shell version: 2.4.2
connecting to: test
> printjson({this: 'is a test'})
{ "this" : "is a test" }
> printjson({this: 'is another test'})
{ "this" : "is another test" }
> exit
bye
Then you'll get a file with this content:
MongoDB shell version: 2.4.2
connecting to: test
> printjson({this: 'is a test'})
{ "this" : "is a test" }
> printjson({this: 'is another test'})
{ "this" : "is another test" }
> exit
bye
To remove all the commands and keep only the json output, you can use a command similar to:
tail -n +3 file.txt | egrep -v "^>|^bye" > output.json
Then you'll get:
{ "this" : "is a test" }
{ "this" : "is another test" }
We can do it this way -
mongo db_name --quiet --eval 'DBQuery.shellBatchSize = 2000; db.users.find({}).limit(2000).toArray()' > users.json
The shellBatchSize argument is used to determine how many rows is the mongo client allowed to print. Its default value is 20.
If you invoke the shell with script-file, db address, and --quiet arguments, you can redirect the output (made with print() for example) to a file:
mongo localhost/mydatabase --quiet myScriptFile.js > output
There are ways to do this without having to quit the CLI and pipe mongo output to a non-tty.
To save the output from a query with result x we can do the following to directly store the json output to /tmp/x.json:
> EDITOR="cat > /tmp/x.json"
> x = db.MyCollection.find(...).toArray()
> edit x
>
Note that the output isn't strictly Json but rather the dialect that Mongo uses.
In the new mongodb shell 5.0+ mongosh, it integrate the Node.js fs module, so you can simply do below in the new mongosh shell:
fs.writeFileSync('output.json', JSON.stringify(db.collectionName.findOne()))
This also avoid problems such as the ObjectId(...) being included in the tojson result, which is not valid JSON string.
The above code works according to the docs describes:
The MongoDB Shell, mongosh, is a fully functional JavaScript and Node.js 14.x REPL environment for interacting with MongoDB deployments. You can use the MongoDB Shell to test queries and operations directly with your database.
The old mongo shell already marked as Legacy, so use the mongosh if possible.
It may be useful to you to simply increase the number of results that get displayed
In the mongo shell > DBQuery.shellBatchSize = 3000
and then you can select all the results out of the terminal in one go and paste into a text file.
It is what I am going to do :)
(from : https://stackoverflow.com/a/3705615/1290746)
Combining several conditions:
write mongo query in JS file and send it from terminal
switch/define a database programmatically
output all found records
cut initial output lines
save the output into JSON file
myScriptFile.js
// Switch current database to "mydatabase"
db = db.getSiblingDB('mydatabase');
// The mark for cutting initial output off
print("CUT_TO_HERE");
// Main output
// "toArray()" method allows to get all records
printjson( db.getCollection('jobs').find().toArray() );
Sending the query from terminal
-z key of sed allows treat output as a single multi-line string
$> mongo localhost --quiet myScriptFile.js | sed -z 's/^.*CUT_TO_HERE\n//' > output.json

How do I clear the command history in the mongo shell

Is there a command to clear the history from within the mongo shell?
There should be a file called .dbshell in your home folder. Just delete it.
MongoDB terminal history is stored inside the ~/.dbshellfile. Just empty the .dbshell file
$ > ~/.dbshell
Versions of Windows mongo.exe earlier than 2.2.0 will save the .dbshell file in the mongo.exe working directory.
You can clear contents on the shell by Ctrl + L
Command history is stored in ~/.dbshell file.
For example, on a Unix-like system:
# Ensure an empty history file
echo "" > ~/.dbshell
# Remove rwx access to the history file
chmod 0 ~/.dbshell
If you're using MongoDB Shell as a standalone package, you will find the shell history file located in ~/.dbshell
If you're using the embedded MongoDB Shell inside MongoDB Compass, you will find the shell history file located in ~/.config/MongoDB Compass/MongoDB Compass/shell-history.json
Just empty the history file