Unable to get Mongo database script output to standard output

51 views Asked by At

I'm able to to see the output of the count() on a DB collection like below:

docker exec -ti STG-mongo-rs mongo --port 27065   --authenticationDatabase '$external' --authenticationMechanism PLAIN -u mydbuser 

> use PP_Metadata;

> db.items.find().count();

947

As you can see i get the output as 947

I now have to get the same output by saving the command db.items.find().count(); in a script and executing the script on mongo DB

cat script1.txt

db.items.find().count();

Below is how i execute the script:

sudo docker exec -ti STG-mongo-rs mongo --port 27065 --authenticationDatabase '$external' --authenticationMechanism PLAIN -u 'mydbuser' -p 'mypassx' --eval "db = db.getSiblingDB('PP_Metadata'); print(db.getName()); load('audit/db_execute_scripts/script1.txt');"

MongoDB shell version v4.2.20

connecting to: mongodb://127.0.0.1:27078/?authMechanism=PLAIN&authSource=%24external&compressors=disabled&gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("74646e6b-9c39-4986-a413-d83dabcbabf0") }

MongoDB server version: 4.2.20

PP_Metadata

true

As you can see the script executes successfully but i get true in the output instead of 947

How can i get the script execution output i.e 947 on command prompt where i run my mongo docker command non-interactively.

The solution i m looking for is to get the output of any script execution on command prompt of non-interactive mongo docker command.

I would eventually use the solution in ansible task.

0

There are 0 answers