Mongoexport issue cannot export single document

Hi Team,
I was try single document address collection backup export on Json getting error .
Can you please let what’s wrong below mongoexport command

H:\Desktop\Downloads\bin>mongoexport.exe --host localhost --port 27017 -u “root” -p “123” --authenticationDatabase admin --db ibm --collection address --query ‘{“name”:“rommel”}’ --out H:\Desktop\Downloads\address.json
2022-02-16T16:20:33.978+0530 query ‘[39 123 110 97 109 101 58 114 111 109 109 101 108 125 39]’ is not valid JSON: json: cannot unmarshal string into Go value of type map[string]interface {}
2022-02-16T16:20:33.978+0530 try ‘mongoexport --help’ for more information

rs1:PRIMARY> db.address.find({“name”:“rommel”})
{ “_id” : ObjectId(“61c4ad4e8960551298a85a91”), “name” : “rommel”, “blk_no” : 12, “street” : “dewey street”, “city” : “olongapo” }
rs1:PRIMARY>

On Windows you have to use double quotes outside.Modify as below for query portion

–query “{‘name’:‘rommel’}”

changed double quotes but getting error:
H:\Desktop\Downloads\bin>mongoexport.exe --host localhost:27017 -u “root” -p “123” --authenticationDatabase admin --db ibm --collection address -q “{‘name’ : ‘rommel’}” -o H:\Desktop\Downloads\address.json
2022-02-16T21:21:32.018+0530 connected to: mongodb://localhost:27017/
2022-02-16T21:21:32.022+0530 Failed: error parsing query as Extended JSON: invalid JSON input

Why --query became -q in your second run?

I used same command again error:
mongoexport.exe --host localhost --port 27017 -u “root” -p “123” --authenticationDatabase admin --db ibm --collection address --query “{‘name’:‘rommel’}” --out H:\Desktop\Downloads\address.json
2022-02-16T21:29:09.568+0530 connected to: mongodb://localhost:27017/
2022-02-16T21:29:09.572+0530 Failed: error parsing query as Extended JSON: invalid JSON input

It may be quotes issue.They look different
Use straight quotes (type it don’t cut & paste)

I did not cut and past I did use straight but same error

The below are the mongo version

H:\Desktop\Downloads\bin>mongoexport --version
mongoexport version: r4.2.14

Please check again.Paste a screenshot

FYI:I was able to export your collection with above revised syntax

C:\Users\xyz>mongoexport --db test --collection address1 --query “{‘name’:‘rommel’}” --out C:\Users\xyz\address1.json
2022-02-16T21:08:13.080+0530 connected to: localhost
2022-02-16T21:08:13.084+0530 exported 1 record

02/16/2022 09:08 PM 116 address1.json

C:\Users\xyz>more address1.json
{"_id":{"$oid":“61c4ad4e8960551298a85a91”},“name”:“rommel”,“blk_no”:12.0,“street”:“dewey street”,“city”:“olongapo”}

mongoexport version: r4.0.5

1 Like

Hi Rama,

Thanks for help …

Now it is working i just changed as below 2 different commands with username and pwd

H:\Desktop\Downloads\bin>mongoexport --db ibm --collection address --query “{“name”: “rommel”}” --host localhost:27017 -u root -p 123 --authenticationDatabase admin --out H:\Desktop\Downloads\hubdwarecords1.json
2022-02-17T23:36:11.567+0530 connected to: mongodb://localhost:27017/
2022-02-17T23:36:11.577+0530 exported 1 record

H:\Desktop\Downloads\bin>mongoexport --host localhost:27017 --db ibm --collection address --query “{“name”: “rommel”}” -u root -p 123 --authenticationDatabase admin --out H:\Desktop\Downloads\hubdwarecords1.json
2022-02-17T23:37:17.802+0530 connected to: mongodb://localhost:27017/
2022-02-17T23:37:17.810+0530 exported 1 record