0

I am using a windows batch file that will output a .js file into csv.

The .js file is a mongo query.

mongosh --host 10.1.0.1:27017 --username "username" --password "password" --authenticationDatabase admin --quiet C:\ActiveUsers1.js > C:\ActiveUsers.csv 
mongosh --host 10.1.0.1:27017 --username "username" --password "password" --authenticationDatabase admin --quiet C:\ActiveUsers2.js >> C:\ActiveUsers.csv
mongosh --host 10.1.0.1:27017 --username "username" --password "password" --authenticationDatabase admin --quiet C:\ActiveUsers3.js >> C:\ActiveUsers.csv

Sample result for each .js file

ActiveUsers1.js = 10
ActiveUsers2.js = 15
ActiveUsers3.js = 20

What I want to have on my ActiveUsers.csv file should be:

10
15
20

What I am getting is:

101520

I just upgraded into mongosh and I am not encountering this when using the old mongo shell (mongo).

Is there a way that I could control this on a .bat file?

1 Answer 1

1

Add echo. after each mongosh to append a carriage return after each line.

    mongosh --host 10.1.0.1:27017 --username "username" --password "password" --authenticationDatabase admin --quiet C:\ActiveUsers1.js > C:\ActiveUsers.csv 
    echo.  >> C:\ActiveUsers.csv  
    mongosh --host 10.1.0.1:27017 --username "username" --password "password" --authenticationDatabase admin --quiet C:\ActiveUsers2.js >> C:\ActiveUsers.csv
    echo.  >> C:\ActiveUsers.csv
    mongosh --host 10.1.0.1:27017 --username "username" --password "password" --authenticationDatabase admin --quiet C:\ActiveUsers3.js >> C:\ActiveUsers.csv
    echo.  >> C:\ActiveUsers.csv 
2
  • Hi @RealHowTo I got it to work. However there is a single space after the ActiveUsers1.js and ActiveUsers2.js is there a way to remove this?
    – JRA
    Commented Sep 9, 2022 at 3:54
  • 1
    Okay I got it to work. I just removed the spacing after echo. >> C:\ActiveUsers.csv into echo.>> C:\ActiveUsers.csv
    – JRA
    Commented Sep 9, 2022 at 4:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .