how to export mongodb query to csv

164 Views Asked by At

not able to execute below query on linux server with mongoDb

mongoexport --db MongoLog --collection dCDMongoLog --query '{$and:[{'jsonObject.responseRaw':{$regex:'<Amount>1000'}},{'jsonObject.createDate':{$gt:'2021-04-01'}},{'jsonObject.createDate':{$lte:'2021-04-08'}}]}' --csv -f jsonObject.mobile,jsonObject.createDate --out /opt/data1234.csv

getting below error

-bash: Amount: No such file or directory

even tried this way :

mongoexport -d MongoLog -c dCDMongoLog -q "{$and:[{'jsonObject.responseRaw':{$regex:'<Amount>1000'}},{'jsonObject.createDate':{$gt:'2021-04-01'}},{'jsonObject.createDate':{$lte:'2021-04-08'}}]}" -f jsonObject.mobile,jsonObject.createDate --type=csv > /opt/data1234.csv

but getting this error:

 error validating settings: query '[123 58 91 123 39 106 115 111 110 79 98 106 101 99 116 46 114 101 115 112 111 110 115 101 82 97 119 39 58 123 58 39 60 65 109 111 117 110 116 62 49 48 48 48 39 125 125 44 123 39 106 115 111 110 79 98 106 101 99 116 46 99 114 101 97 116 101 68 97 116 101 39 58 123 58 39 50 48 50 49 45 48 52 45 48 49 39 125 125 44 123 39 106 115 111 110 79 98 106 101 99 116 46 99 114 101 97 116 101 68 97 116 101 39 58 123 58 39 50 48 50 49 45 48 52 45 48 56 39 125 125 93 125]' is not valid JSON: invalid character ':' looking for beginning of object key string
2021-04-16T20:01:00.729-0400    try 'mongoexport --help' for more information

nothing seems to be running. Any other ways?

1

There are 1 best solutions below

0
On

mongoexport query sucks! it requires single quotes outside and double quotes inside. below command worked for me.

mongoexport -d MongoLog -c dCDMongoLog -q '{"$and":[{"jsonObject.responseRaw":{$regex:"<Amount>1000"}},{"jsonObject.createDate":{$gt:"2021-04-01"}},{"jsonObject.createDate":{$lte:"2021-04-08"}}]}' -f jsonObject.mobile,jsonObject.createDate --type=csv > /opt/data1235.csv