Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get output of mongo shell script

Tags:

mongodb

as a part of my mongoDB maintenance I'm running mongo shell and make it to load 2 scripts. The command I'm running looks like follows:

$MONGO_HOME/bin/mongo --verbose --port 27017 replSetConfig.js initializeReplicaSet.js

The output I got is:

MongoDB shell version: 2.2.3
Thu Mar  7 03:00:00 versionCmpTest passed
Thu Mar  7 03:00:00 versionArrayTest passed connecting to: 127.0.0.1:27017/test
Thu Mar  7 03:00:01 creating new connection to:127.0.0.1:27017
Thu Mar  7 03:00:01 BackgroundJob starting: ConnectBG
Thu Mar  7 03:00:01 connected connection!
loading file: js/replSet.config.js
loading file: js/initializeReplicaSet.js

I'm redirecting the output to a log file but I would like to see some output of the loaded scripts as well. I.e. the output which I see in the shell if I start it and call load("...") for the very same scripts. Is there a way how to capture the output?

Thanks

like image 817
Jan Zyka Avatar asked Mar 07 '13 10:03

Jan Zyka


People also ask

What is the output of MongoDB?

The MongoDB Output tool is used for writing data to MongoDB databases. MongoDB is a scalable, high-performance, open source NoSQL database. Learn more about MongoDb. MongoDB databases store data in a binary JSON format called BSON.

How do I run a Mongosh script?

You can use mongosh to execute a script from the command line without entering the mongosh console. To specify the filename, use the --file or -f parameter to specify the filename.

What does find () do in MongoDB?

In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents.


1 Answers

To add output of scripts you must use print() or printjson() statements otherwise MongoDB will remain quiet about any output of a script.

like image 54
Sammaye Avatar answered Sep 30 '22 13:09

Sammaye