MongoExport with query

MongoExport is working without query. when i try the below query getting erros
Shell version 4.2.8

-q=’{ “CreatedOnUtc”: { “$lt”: { “$date”: “2016-01-01T00:00:00.000Z” } } }’

Error message: error parsing command line options: too many positional arguments: [CreatedOnUtc: { $lt: { $date: 2016-01-01T00:00:00.000Z } } }’]

post screenshot of the whole command. your quotes are not shown correctly.

Also tried this

Hi @Ezi welcome to the community!

This is a common issue in Windows, since cmd doesn’t recognize single quotes as delimiters, unlike Linux.

Could you try switching your quotes around (single quotes to double quotes and vice versa) like this and see if it works?

-q="{ 'CreatedOnUtc': { '$lt': { '$date': '2016-01-01T00:00:00.000Z' } } }"

Best regards

Thanks @kevinadi . When i try , still getting error

When I tried this query on my Windows it did not give any errors. Gave the message exported zero rows since I don’t have that collection nor field
Can you add some sample docs
Sometimes spaces cause issues and interpreted differently.Remove extra space after date:
Put your query in a json file and use queryfile option of mongoexport

When collection has zero row. No error. once have the error result have at least one doc.


SET MONGOEXPORT="%ProgramFiles%\Alteryx\bin\mongoexport.exe"
FOR /f %%a IN (‘WMIC OS GET LocalDateTime ^| FIND “.”’) DO SET DTS=%%a
SET Date=%DTS:~0,4%-%DTS:~4,2%-%DTS:~6,2%
SET Time=%DTS:~8,2%:%DTS:~10,2%:%DTS:~12,2%%DTS:~14,4%Z
SET /a tztemp=%DTS:~21%/60
SET tzone=UTC%tztemp%
SET DateTime=%DTS:~0,4%%DTS:~4,2%%DTS:~6,2%_%DTS:~8,2%%DTS:~10,2%%DTS:~12,2%

SET JSON="{‘Timestamp’:{’$gte’:{’$date’:’%Date%T00:00:00.000Z’}}}"

%MONGOEXPORT% --port=27018 --username=user --password=%PS% --db=AlteryxGallery --collection=auditEvents --query=%JSON% >>%OUTPUT%AuditEvents%datetime%.csv

I was using this batch script to export audit events in Mongodb4.0. now my environment is migrated to 4.2 and 4.4 . If i run the query now it s giving below error.

2022-09-14T12:33:33.389+0100 connected to: mongodb://localhost:27018/
2022-09-14T12:33:33.390+0100 Failed: error parsing query as Extended JSON: invalid JSON input

Can you clarify why and what modification should i do

Your set JSON variable on timestamp seems to be the issue
For extended json you have to enclose all fields,functions in double quotes and single quote outside the flower bracket
Check documentation for exact syntax/example


SET JSON=’{“Timestamp”:{"$gte":{"$date":"%Date%T00:00:00.000Z"}}}’

I have tried this as well but no luck

2022-09-15T09:52:01.804+0100 query ‘[39 123 84 105 109 101 115 116 97 109 112 58 123 36 103 116 101 58 123 36 100 97 116 101 58 50 48 50 50 45 48 57 45 49 53 84 48 48 58 48 48 58 48 48 46 48 48 48 90 125 125 125 39]’ is not valid JSON: json: cannot unmarshal string into Go value of type map[string]interface {}

What is your os?
If it is Windows format will be different
Also your quotes around Timestamp look different.Use double quotes
Single quote outside flower bracket also look different
Check this thread for Windows query format
Mongodump --query not able to filter using timestamp