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:
JSON.stringify(db.getSiblingDB('survey').getCollection('details').find({age:{$gt:60}}).toArray())
Or something similar.
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.