Mongodb storing file into .txt

I have loaded a json file in mongo then i have performed some operation on that data and i have to save the query result into a textfile but when i am runing that query in mongo shell it is working fine and showing me data as per the condition but when i come out of mongo shell and try to store my query data into txt file it is showing syntax error. I am using eco

What error are you getting and how are you trying to extract the data? Redirect console output from a shell or using mongoexpprt or something else?

Redirecting consol output from shell .

And whats the error? Whats the command you are running?

(echo “use survey”; echo “db.details.find({age:{$gt:60}})”) | mongo > medical.txt

In medical.txt file its showing syntax error

Can you paste the error exactly you’re getting?

Try doing:


Or something similar.

Attaching image of the error accuring. Please let me know if you find the image

This is the command i am using

I could not get that working like that either on a command line, however this does work:

mongosh --quiet --eval "db.getSiblingDB('survey').getCollection('details').find()"

I’d recommend swapping to the new mongosh as opposed to the legacy mongo shell, for a start the quiet option now works so doing this results in output that does not have extra text.

You could also use mongoexport:

With the filtering options.